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


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>

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.


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

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

auto lo
iface lo inet loopback
iface lo inet static

or to temporarily test

ip address add dev lo
  • 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
    Commented 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
    Commented Aug 20, 2020 at 22:05
  • So the above workaround to provide a consistent address across all containers
    – Matt
    Commented Aug 20, 2020 at 22:05

Your Answer

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

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