2

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 ?

1
  • 1
    Check my answer for a proper solution. Better late than nothing ^^ Commented Aug 29, 2023 at 4:37

3 Answers 3

3

warning Do not use 0.0.0.0 to bind a socket on your host. It can be a security issue. It's the way to declare all IP are able to connect to mongodb from any host.

Better edit /etc/mongod.conf and add the docker interface ip, like:

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,172.17.0.1

Then, in the docker run, you can add a host:

docker run --add-host mongohost:172.17.0.1 <CONTAINER>

Now, in your container, you can query mongohost on port 27017.

This is the clean solution.

You can also use extra_hosts if you use docker-compose.

But, Docker Compose is primarily designed for local development and testing purposes.

According to the official Docker documentation, if you insist to use it, use V2 and a special production.yaml

Sign up to request clarification or add additional context in comments.

Comments

3

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.

This solution is not provided for every docker environments. docker desktop have this feature unlike basic Linux environment.

4 Comments

same problem :/
@patacoing Edit your question and show how you are running your container with all options, etc. If your container is set up correctly, this answer is correct.
Edited post, this solution is not working outside of docker desktop. See my answer
Using docker desktop and this is cleaner. Just replace localhost:PORT with host.docker.internal:PORT
-1

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.

5 Comments

Anything Docker Compose can put together so can Docker. Using Docker Compose is a good tool but by itself does not solve the problem.
But 172.17.0.1 refers to my host machine, so there should be a mongo service running
@JohnHanley: 172.17.0.1 is typically the ip of the docker interface by default, so it's the ip of the host. See my full answer.
@GillesQuénot - Thank you for the feedback. +1 for your answer that mentions security with a good solution.
Not really a solution, no example, moreover, not the cleanest way. Sorry but voted down.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.