skip to Main Content

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


  1. 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:

    I want to connect from a container to a service on the host
    The host has a changing IP address (or none if you have no network access). We recommend that you connect to the special DNS name host.docker.internal which resolves to the internal IP address used by the host. This is for development purpose and does not work in a production environment outside of Docker Desktop.

    Login or Signup to reply.
  2. 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 at 172.0.0.1:27017 of the ubuntu container.

    Docker-compose is the right tool for you to make containers communication to each other.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search