Some of our customers was unable to connect to MongoDB in a docker container.
He received the below error message:
warning: Failed to connect to ip:27017, reason: errno:61 Connection refused.
You have to tell the container to use it's own IP Address instead of localhost.
How to fix the error "Unable to connect to MongoDB in docker container" ?
Recently, one of our customers approached us saying that he was trying to connect to MongoDB with mongo ip:27017 (where IP is the name from boot2docker IP) + the port number from docker ps:
or with -P
Either way, he got the below 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
Here we found that the customer was confused with the internal port with the public port.
So, we suggested our customer to use the below command:
This resolved the error.
We also, provide the below steps to customers if they are on Mac and using Docker Machine:
1. First, get the name of the VM running docker daemon.
For that, run the below command.
$ docker-machine ls
2. Next, get the VM’s IP info. For that, run the below command.
$ docker-machine env
3. Finally, connect with the mongo client to the VM IP and the mongo mapped port. For that, run the below command.
$ mongo VM-IP:port