I have a mongo on my host machine, and an ubuntu container which is also running on my machine. I want that container to connect to mongo.
I set as host url, my host ip from docker network : 172.17.0.1
and in the /etc/mongod.conf file I set the bindIp to 0.0.0.0
from the container, I can ping the host,but the mongo service is not accessible, I get that error :
Connecting to: mongodb://172.17.0.1:27017/directConnection=true&appName=mongosh+1.5.0
MongoServerSelectionError: connection timed out
More over, I can connect from host to the mongo service with that command :
mongosh mongodb://172.17.0.1:27017
Do you know why I can’t access mongo service from my container ?
2
Answers
You can use host.docker.internal as reference to your host machine from the container. So
mongo host.docker.internal:27017
should work.From docker documentation:
On your local machine that has Mongo service is running, you can access by Mongo client because you expose the service at
127.0.0.1:27017
.However, it is not true if standing from your
unbuntu
container, there is no Mongo service is running at172.0.0.1:27017
of theubuntu
container.Docker-compose is the right tool for you to make containers communication to each other.