12

I've been on this issue for hours now and I really don't know what else to do (and all the research I did didn't provide any solution unfortunately). So I'm asking if anyone can think of an answer why this is not working:

I run a docker-compose file with mongo and mongo-express images. Using the default code provided here didnt work ( mongo-express stopped with exit code 1) so I tried tons of configurations leaving me with the docker-compose file below ( that actually works to the degree that mongo-express gets started and shows

Databases: admin

Server Status: Turn on admin in config.js to view server stats!

displayed in the GUI). But from there nothing works. The console shows the problem:

mongoviewer_1  | Database connected
database_1     | 2018-08-22T09:15:27.743+0000 I ACCESS   [conn2] SASL SCRAM-SHA-1 authentication failed for admin on admin from client; UserNotFound: Could not find user admin@admin

mongoviewer_1  | unable to list databases
mongoviewer_1  | { MongoError: command listDatabases requires authentication
mongoviewer_1  |     at Function.MongoError.create (/node_modules/mongodb-core/lib/error.js:31:11)
mongoviewer_1  |     at /node_modules/mongodb-core/lib/connection/pool.js:483:72
mongoviewer_1  |     at authenticateStragglers (/node_modules/mongodb-core/lib/connection/pool.js:429:16)
mongoviewer_1  |     at Connection.messageHandler (/node_modules/mongodb-core/lib/connection/pool.js:463:5)
mongoviewer_1  |     at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:319:22)
mongoviewer_1  |     at emitOne (events.js:116:13)
mongoviewer_1  |     at Socket.emit (events.js:211:7)
mongoviewer_1  |     at addChunk (_stream_readable.js:263:12)
mongoviewer_1  |     at readableAddChunk (_stream_readable.js:250:11)
mongoviewer_1  |     at Socket.Readable.push (_stream_readable.js:208:10)
mongoviewer_1  |   name: 'MongoError',
mongoviewer_1  |   message: 'command listDatabases requires authentication',
mongoviewer_1  |   ok: 0,
mongoviewer_1  |   errmsg: 'command listDatabases requires authentication',
mongoviewer_1  |   code: 13,
mongoviewer_1  |   codeName: 'Unauthorized' }

The docker-compose looks like follows:

  database:
    image: mongo:latest
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: password
      MONGO_INITDB_DATABASE: dockerdb
    ports:
    - "27017:27017"
  mongoviewer:
    image: mongo-express
    environment:
      ME_CONFIG_MONGODB_SERVER: database
      ME_CONFIG_BASICAUTH_USERNAME: ""
      ME_CONFIG_MONGODB_AUTH_DATABASE: admin
      ME_CONFIG_MONGODB_AUTH_USERNAME: admin
      ME_CONFIG_MONGODB_AUTH_PASSWORD: password
      ME_CONFIG_MONGODB_ADMINUSERNAME: admin
      ME_CONFIG_MONGODB_ADMINPASSWORD: password
    ports:
    - "8081:8081"
    links:
    - database
    depends_on:
    - database

That's the current version I was trying and I tried so many other configurations but nothing fixed the auth problem.

If anyone has an answer or at least an idea on what to do I'd be extremely thankfull!

3 Answers 3

5

When we login using localhost:8081 it asks username and password , when we give declared username and pwd it fails. rather pls execute docker logs containerID/containerName and see basic credentials printed. Kindly use the mentioned credentials , admin:pass. Hope it solves the error Docker Log

Sign up to request clarification or add additional context in comments.

Comments

2

Essentially there are two states when you are configuring your docker-compose.yaml. I am no expert but this solution resolved this issue for me.

  1. No Authentication Required

By default when you're running mongo instance, it requires authentication to access the databases since you are specifying both MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD.

You can access the databases in mongo by removing both of the keys.

  1. Authentication Required

According to mongo-express documentation, by default the ME_CONFIG_MONGODB_ENABLE_ADMIN key is false making your mongo-express application will not try for any authentication and directly connect to your mongo instance (thus producing the Authentication failed error).

You will need to enable the ME_CONFIG_MONGODB_ENABLE_ADMIN key by changing it to true. Remember to stringify the "true" in your docker-compose.yaml.

Ensure the ME_CONFIG_MONGODB_ADMINUSERNAME and ME_CONFIG_MONGODB_ADMINPASSWORD keys match MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD keys respectively.

Comments

0

Based on the info on the docker file, if MongoDB runs on a container named "database", it can be accessed from mongo-express using:

 docker run -it --rm \
--network web_default \
--name mongo-express \
-p 8081:8081 \
-e ME_CONFIG_MONGODB_SERVER=database \
-e ME_CONFIG_MONGODB_ADMINUSERNAME= admin \
-e ME_CONFIG_MONGODB_ADMINPASSWORD=password \
mongo-express

For reference check the mongo-express Docu on dockerhub

Note: that in case you want to access a specific database with a user credentials:

docker run -it --rm \
--network web_default \
--name mongo-express \
-p 8081:8081 \
-e ME_CONFIG_MONGODB_SERVER= database \
-e ME_CONFIG_MONGODB_AUTH_DATABASE= dockerdb \
-e ME_CONFIG_MONGODB_ENABLE_ADMIN=false \
-e ME_CONFIG_MONGODB_AUTH_USERNAME=admin \
-e ME_CONFIG_MONGODB_AUTH_PASSWORD=password \
mongo-express
 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.