2

I am trying to setup a Mongo database with authentication inside a docker container. But no matter what I do I keep getting an error saying Authentication failed when I try to connect with my MongoDB.

docker-compose.yml

version: '3.4' 

services:
  catalogdb:
    image: mongo
    container_name: catalogdb
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: secret
      MONGO_INITDB_DATABASE: CatalogDb
    ports:
    - "7000:27017"
    volumes:
    - mongo_data:/data/db 

  dnms.catalog.api:
    image: ${DOCKER_REGISTRY-}dnmscatalogapi
    build: 
      context: .
      dockerfile: Services/Catalog/DNMS.Catalog.API/Dockerfile
    container_name: dnms.catalog.api
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
    depends_on:
      - catalogdb
    ports:
      - "8000:80"

volumes:
  mongo_data:

When I open my container shell (docker exec -it catalogdb /bin/bash) and try to open the mongo shell (mongo CatalogDb -u root -p secret) it is giving me the authentication error. When I try to connect to the database via my application with a connection string (mongodb://root:secret@catalogdb:27017/CatalogDb?authSource=admin) I get Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1.. Connecting with MongoDB Compass (mongodb://root:secret@localhost:7000/CatalogDb?authSource=admin) also gives me an authentication failed error.

if I don't use authentication I can connect just fine, but I really want to secure my database. I've also tried adding a mongo init script with a volume mapping to docker-entrypoint-initdb.d, but I keep getting the same errors. What do I have to do?

Jiren
  • 536
  • 7
  • 24

0 Answers0