0

Hi i’m trying to communicate with mongodb in the host from a flask container…

So using “docker.host.internal” on the URI didn’t work (host machine is Ubuntu)

Any suggestions ?? To solve this

3 Answers 3

1

You can run your flask container using the host networking mode. This way docker won't isolate the container and will run it the same network namespace as the host, and you container will be able to discover your database using localhost as mongodb hostname

docker run --network host <flask-image>
1
0

From the point of view of your MongoDB instance and your Flask app they're running on separate servers, so the steps to make the former visible to the latter are the same as if they were running on two different physical servers.

  1. Check if the servers are on the same LAN - they are (docker bridge network).

  2. Find if MongoDB is configured to listen on all network interfaces. If not either configure it to do so or to listen on the interface that is connected to the LAN both servers have in common.

  3. If you haven't already, find what IP your MongoDB server has on the common LAN and set Flask app to use it to connect to the database.

  4. Optionally, if your MongoDB server has a firewall, make sure that whatever port MongoDB is listening on is not blocked.

-1

You can add an alias IP address to the loopback interface on the host to get a consistent address to use from your containers.

Any private address you won't see on the local or container networks, something like 10.8.8.8

Edit /etc/network/interfaces.d/lo for a permanent address after reboot

auto lo
iface lo inet loopback
iface lo inet static
  address 10.8.8.8/32

or to temporarily test

ip address add 10.8.8.8/32 dev lo
3
  • I will try it.... But the question is where's docker and ubuntu guys !! Many people got the same problem and there are no "official" solution like for mac and Windows
    – user23619
    Aug 20, 2020 at 17:09
  • Docker for Windows/Mac inject the docker.host.internal address into DNS as there is no way to find the VM hypervisors (hosts) address from inside the VM. When you are on the host running docker, the gateway address in the container was historically used.
    – Matt
    Aug 20, 2020 at 22:05
  • So the above workaround to provide a consistent address across all containers
    – Matt
    Aug 20, 2020 at 22:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.