title: Mongo authentication inside Docker
body: <p>I am trying to run the mongo docker image with authentication. Following the most simple example from the <a href="https://docs.docker.com/samples/library/mongo/#start-a-mongo-server-instance" rel="noreferrer">documentation</a> I ran the mongo and the mongo-express images by the <code>docker-compose up</code> command. My <code>docker-compose.yml</code> at this stage:</p>
  
  <pre><code>version: '3.1'
  
  services:
  
    mongo:
      image: mongo
      restart: always
      environment:
        MONGO_INITDB_ROOT_USERNAME: root
        MONGO_INITDB_ROOT_PASSWORD: example
  
    mongo-express:
      image: mongo-express
      restart: always
      ports:
        - 8081:8081
      environment:
        ME_CONFIG_MONGODB_ADMINUSERNAME: root
        ME_CONFIG_MONGODB_ADMINPASSWORD: example
  </code></pre>
  
  <p>This runs, both containers start ok and I can browse the contents of mongo from the mongo-express website. However, whenever I change the username or the password in the <code>docker-compose.yml</code> file, for example to this:</p>
  
  <pre><code>version: '3.1'
  
  services:
  
    mongo:
      image: mongo
      restart: always
      environment:
        MONGO_INITDB_ROOT_USERNAME: root
        MONGO_INITDB_ROOT_PASSWORD: example123
  
    mongo-express:
      image: mongo-express
      restart: always
      ports:
        - 8081:8081
      environment:
        ME_CONFIG_MONGODB_ADMINUSERNAME: root
        ME_CONFIG_MONGODB_ADMINPASSWORD: example123
  </code></pre>
  
  <p>the mongo-express throws an unauthotrized error message:</p>
  
  <pre><code>mongo-express_1  | Admin Database connected
  mongo-express_1  | { MongoError: Authentication failed.
  mongo-express_1  |     at Function.MongoError.create (/node_modules/mongodb-core/lib/error.js:31:11)
  mongo-express_1  |     at /node_modules/mongodb-core/lib/connection/pool.js:483:72
  mongo-express_1  |     at authenticateStragglers (/node_modules/mongodb-core/lib/connection/pool.js:429:16)
  mongo-express_1  |     at Connection.messageHandler (/node_modules/mongodb-core/lib/connection/pool.js:463:5)
  mongo-express_1  |     at Socket.&lt;anonymous&gt; (/node_modules/mongodb-core/lib/connection/connection.js:319:22)
  mongo-express_1  |     at emitOne (events.js:116:13)
  mongo-express_1  |     at Socket.emit (events.js:211:7)
  mongo-express_1  |     at addChunk (_stream_readable.js:263:12)
  mongo-express_1  |     at readableAddChunk (_stream_readable.js:250:11)
  mongo-express_1  |     at Socket.Readable.push (_stream_readable.js:208:10)
  mongo-express_1  |   name: 'MongoError',
  mongo-express_1  |   message: 'Authentication failed.',
  mongo-express_1  |   ok: 0,
  mongo-express_1  |   errmsg: 'Authentication failed.',
  mongo-express_1  |   code: 18,
  mongo-express_1  |   codeName: 'AuthenticationFailed' }
  mongo-express_1  | unable to list databases
  mongo-express_1  | { MongoError: command listDatabases requires authentication
  mongo-express_1  |     at Function.MongoError.create (/node_modules/mongodb-core/lib/error.js:31:11)
  mongo-express_1  |     at /node_modules/mongodb-core/lib/connection/pool.js:483:72
  mongo-express_1  |     at authenticateStragglers (/node_modules/mongodb-core/lib/connection/pool.js:429:16)
  mongo-express_1  |     at Connection.messageHandler (/node_modules/mongodb-core/lib/connection/pool.js:463:5)
  mongo-express_1  |     at Socket.&lt;anonymous&gt; (/node_modules/mongodb-core/lib/connection/connection.js:319:22)
  mongo-express_1  |     at emitOne (events.js:116:13)
  mongo-express_1  |     at Socket.emit (events.js:211:7)
  mongo-express_1  |     at addChunk (_stream_readable.js:263:12)
  mongo-express_1  |     at readableAddChunk (_stream_readable.js:250:11)
  mongo-express_1  |     at Socket.Readable.push (_stream_readable.js:208:10)
  mongo-express_1  |   name: 'MongoError',
  mongo-express_1  |   message: 'command listDatabases requires authentication',
  mongo-express_1  |   ok: 0,
  mongo-express_1  |   errmsg: 'command listDatabases requires authentication',
  mongo-express_1  |   code: 13,
  mongo-express_1  |   codeName: 'Unauthorized' }
  </code></pre>
  
  <p>No matter what username or password I enter in <code>docker-compose.yml</code>, I cannot make mongo-express connect to mongo, only if I use the original <code>root</code> and <code>example</code> pair. </p>
  
  <p>Note, that I am not getting the username and password as environment variables, but they are directly typed into the <code>docker-compose.yml</code> file as you can see here.</p>
  
  <p>Also note, that when I change the <code>MONGO_INITDB_ROOT_USERNAME</code> and <code>MONGO_INITDB_ROOT_PASSWORD</code> (mongo's) variables to anything, they don't seem to have an effect, I can still connect with mongo-express using the original root and example credentials.</p>
  
  <p>What causes this behaviour? How can I make this work? </p>