I am trying to setup a local Mongo instance using Docker for development purposes. I need to turn on authentication, and according to the docs it is possible.

The problem is, after I create the initial admin user (using those docs above), the next step is to connect "externally", which fails. I would also like to be able to connect via thick client (I am using Robo 3T).

I created the docker container using:

docker run --name mongodb -p -d mongo --auth

Then I created the admin user with:

docker exec -it mongodb mongo admin
> db.createUser({ user: 'root', pwd: '123456', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });

Then I tried to login externally using:

docker run -it --rm --link mongodb:mongo mongo mongo -u root -p 123456

to wit I get the error:

MongoDB shell version v3.6.2
connecting to: mongodb://
2018-01-26T16:17:34.309+0000 W NETWORK  [thread1] Failed to connect to, in(checking socket for error after poll), reason: Connection refused
2018-01-26T16:17:34.309+0000 E QUERY    [thread1] Error: couldn't connect to server, connection attempt failed :
exception: connect failed

I think I am misunderstanding something, but I am not sure what.

  • I am having the same problem. More specifically I can't seem to expose the MongoDB instance I have running in a Docker container on my Mac to external clients (e.g. Mongo Compass running on a PC). I have not enabled authentication, which is probably a good idea, but I would be happy just to connect from the remote computer. I can connect using Mongo Compass running locally on my Mac. – benhorgen Feb 14 '18 at 16:49

Make sure that the /data/db directory has the right permissions by running

sudo chown -R id -un /data/db

Enter your password

  • I don't have the /data/db volume mounted to my host, and I would hope that the official mongo container would have the permissions setup correctly for it's data folder. – CodeChimp Jan 29 '18 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.