30

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

AJN
  • 1,196
  • 2
  • 19
  • 47
Chris G.
  • 23,930
  • 48
  • 177
  • 302

5 Answers5

25

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.

falsecrypt
  • 1,536
  • 14
  • 11
  • 2
    This did not work for me. Is there a stable version of the mongo docker image perhaps? Buggy buggy buggy stuff here. – user3335999 May 09 '19 at 15:10
18

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

0.0.0.0:49155->27017/tcp

Thanks to the ZeissS.

TheNikCD
  • 191
  • 1
  • 2
  • 18
Chris G.
  • 23,930
  • 48
  • 177
  • 302
  • 4
    Those (like me) who got confused with the answer. use the following to connect. **$mongo IP:49155** – Jardalu May 05 '15 at 07:50
  • 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** – trevorrobertsjr Dec 21 '15 at 15:10
  • @Jardalu I'm still confused – Aleksandrus Aug 22 '16 at 19:57
  • @Aleksandrus replace "IP" with the ip address of your docker machine. See VMTrooper's reply. – Jardalu Sep 30 '16 at 09:29
12

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
trevorrobertsjr
  • 705
  • 7
  • 6
  • 2
    This is a valid answer if you are using the VirtualBox/docker-machine (Windows/Mac) version of Docker. – Flash Aug 12 '16 at 17:44
3

Assuming your mongodb is within a container, for other containers to connect to it, they all need to be on the same network.

To have mongodb and other containers (that want to connect it), create a new network using below command

docker network create --driver bridge my_bridge

Then run mongodb and other containers using the --net flag

docker run --net=my_bridge --name mongodb -p 27017:27017 mongodb
docker run --net=my_bridge --name my-service -p 7002:7002 my-service

Now you should be able to connect mongodb with given alias name from those containers

mongo --host "mongodb:27017"
Anirudha
  • 32,393
  • 7
  • 68
  • 89
2
DATABASE_URI=mongodb://mongo:27017/db_name

Should be the Database URI for a service definition like below (and not mongodb://localhost or mongodb://IP). Use service name or container name.

services  
  mongo:
    container_name: mongo
    image: mongo
    ports: 
      - '27017:27017'