title: Docker MongoDB authentication failed
body: <p>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.</p>
  <p><strong>docker-compose.yml</strong></p>
  <pre><code>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:
      - &quot;7000:27017&quot;
      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:
        - &quot;8000:80&quot;
  
  volumes:
    mongo_data:
  </code></pre>
  <p>When I open my container shell (<code>docker exec -it catalogdb /bin/bash</code>) and try to open the mongo shell (<code>mongo CatalogDb -u root -p secret</code>) it is giving me the authentication error.
  When I try to connect to the database via my application with a connection string (<code>mongodb://root:secret@catalogdb:27017/CatalogDb?authSource=admin</code>) I get <code>Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1.</code>. Connecting with MongoDB Compass (<code>mongodb://root:secret@localhost:7000/CatalogDb?authSource=admin</code>) also gives me an authentication failed error.</p>
  <p>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-<code>entrypoint-initdb.d</code>, but I keep getting the same errors. What do I have to do?</p>