One 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.
Here at Bobcares, we have seen several such errors while troubleshooting Docker issues as part of our Docker Hosting Support for Docker users, web hosts, and online service providers.
Today we’ll take a look at the cause for this error and how to fix it.
Unable to connect to MongoDB in docker container: How we fix
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:
27017/tcp or with -P 0.0.0.0:49155->27017/tcp
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
Now let us see how our Support Engineers tackle this connection issue.
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
[Need any further assistance in fixing Docker errors? – We are here to help you]
In short, we found that this Docker error occurred due to a port issue. Today, we saw the resolution to this error our Support Engineers found.