Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect to mongoDB running in docker container

Tags:

docker

mongodb

Following this example: https://docs.docker.com/engine/examples/mongodb/

When trying to connect to mongoDB with: mongo ip:27017 (where ip is the name from boot2docker ip) + the port number from docker ps:

27017/tcp
or with -P
0.0.0.0:49155->27017/tcp

Either way I get the following errors:

warning: Failed to connect to ip:27017, reason: errno:61 Connection refused

Error: couldn't connect to server ip:27017 (ip), connection attempt failed at src/mongo/shell/mongo.js:148 exception: connect failed

like image 777
Chris G. Avatar asked Sep 13 '14 21:09

Chris G.


People also ask

How do I know if MongoDB is running in Docker container?

If your application is running inside a container itself, you can run MongoDB as part of the same Docker network as your application using --network . With this method, you will connect to MongoDB on mongodb://mongodb:27017 from the other containerized applications in the network.

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.

How do I link my local Docker to MongoDB?

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.

Why MongoDB compass is not connecting?

Ensure Your MongoDB Instance is Running Compass must connect to a running MongoDB instance. Make sure you have installed MongoDB and have a running mongod process. You should also check that the port where your MongoDB instance is running matches the port you provide in the Compass connect dialog.


3 Answers

If you specified the correct port and still not able to connect to mongodb running in docker (like me), make sure you are using the service name (or container name) in your connection URL, e.g. mongodb://mongodb_service:27017/mydb, which is defined in your docker-compose.yml :

services:
  mongodb_service:
    image: mongo

I was using the hostname value and that's not the correct thing to do. You could verify this by looking at docker inspect mongodb_service in the Aliases section.

like image 154
falsecrypt Avatar answered Oct 21 '22 00:10

falsecrypt


I was using port 27017 instead of 49155 (doh, port forwarding)

0.0.0.0:49155->27017/tcp

Thanks to ZeissS

like image 26
Chris G. Avatar answered Oct 21 '22 01:10

Chris G.


If you are on a Mac and using Docker Machine, do the following:

1. Get the name of the VM running docker daemon
$ docker-machine ls

2. Get the VM's IP info
$ docker-machine env 

3. Connect with the mongo client to the VM IP and the mongo mapped port
$ mongo VM-IP:port
like image 12
trevorrobertsjr Avatar answered Oct 21 '22 00:10

trevorrobertsjr