Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to MongoDB running in Docker container?

I'm running a Docker container on MacOS that starts a Postgres server and MongoDB server as follows:

docker run -p 80:80 -p 27017:27017 dockertest
 * Starting PostgreSQL 9.3 database server
  ...done.
 * Starting database mongodb
  ...done.

The output shows that the MongoDB server is started correctly. Then I lookup the IP of the VM in which the container is running, like this:

docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v1.11.2

The ports seem to be mapped:

docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                          NAMES
39e5198e4693        dockertest   "/bin/sh -c 'service "   3 minutes ago       Up 3 minutes        0.0.0.0:80->80/tcp, 0.0.0.0:27017->27017/tcp   drunk_nobel

However, when I want to connect to the MongoDB in another terminal window, using

mongo 192.168.99.100:27017

I get the following error:

MongoDB shell version: 2.6.4
connecting to: 192.168.99.100:27017/test
2016-06-05T18:48:19.050+0200 warning: Failed to connect to      192.168.99.100:27017, reason: errno:61 Connection refused
2016-06-05T18:48:19.052+0200 Error: couldn't connect to server 192.168.99.100:27017 (192.168.99.100), connection attempt failed at    src/mongo/shell/mongo.js:148
exception: connect failed

I already had a look at similar questions (e.g., Unable to connect to mongoDB running in docker container) but the proposed solution does not seem to work.

like image 368
MaVe Avatar asked Jun 05 '16 16:06

MaVe


People also ask

How do I connect to a MongoDB Docker container?

For connecting to your local MongoDB instance from a Container you must first allow to accept connections from the Docker bridge gateway. To do so, simply add the respective gateway IP in the MongoDB config file /etc/mongod. conf under bindIp in the network interface section.

How can I access a MongoDB container from another container?

If you need to access the MongoDB server from another application running locally, you will need to expose a port using the -p argument. Using this method, you will be able to connect to your MongoDB instance on mongodb://localhost:27017 . You can try it with Compass, MongoDB's GUI to visualize and analyze your data.


Video Answer


1 Answers

The problem was not related to Docker, but to the bindip in the configuration of MongoDB. See e.g., mongodb.conf bind_ip = 127.0.0.1 does not work but 0.0.0.0 works

like image 100
MaVe Avatar answered Oct 18 '22 06:10

MaVe