Answers: <p>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 <code>localhost</code> as mongodb hostname</p>
  <pre><code>docker run --network host &lt;flask-image&gt;
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2020-08-25T19:03:20.473Z
Answers: Host network only works for Docker Swarm, not for regular Docker: https://docs.docker.com/network/
Answers: Konrad Botor
Answers: CC BY-SA 4.0
Answers: 2020-08-25T09:26:38.190Z
Answers: 2020-08-25T10:52:49.390Z
Answers: 2020-08-25T10:52:49.390Z
Answers: Iduoad
Answers: <p>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.</p>
  <ol>
  <li><p>Check if the servers are on the same LAN - they are (docker bridge network).</p>
  </li>
  <li><p>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.</p>
  </li>
  <li><p>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.</p>
  </li>
  <li><p>Optionally, if your MongoDB server has a firewall, make sure that whatever port MongoDB is listening on is not blocked.</p>
  </li>
  </ol>
  
Answers: CC BY-SA 4.0
Answers: 2020-08-25T19:14:24.083Z
Answers: 2020-08-25T19:14:24.083Z
Answers: Konrad Botor
Answers: <p>You can add an alias IP address to the loopback interface on the host to get a consistent address to use from your containers.</p>
  <p>Any private address you won't see on the local or container networks, something like 10.8.8.8</p>
  <p>Edit <code>/etc/network/interfaces.d/lo</code> for a permanent address after reboot</p>
  <pre><code>auto lo
  iface lo inet loopback
  iface lo inet static
    address 10.8.8.8/32
  </code></pre>
  <p>or to temporarily test</p>
  <pre><code>ip address add 10.8.8.8/32 dev lo
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2020-08-20T17:09:08.977Z
Answers: 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
Answers: user23619
Answers: CC BY-SA 4.0
Answers: 2020-08-20T22:05:04.467Z
Answers: 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.
Answers: Matt
Answers: CC BY-SA 4.0
Answers: 2020-08-20T22:05:39.647Z
Answers: So the above workaround to provide a consistent address across all containers
Answers: Matt
Answers: CC BY-SA 4.0
Answers: 2020-08-20T06:28:08.687Z
Answers: 2020-08-20T21:59:24.960Z
Answers: 2020-08-20T21:59:24.960Z
Answers: Matt
Body: <p>Hi i’m trying to communicate with mongodb in the host from a flask container…</p>
  <p>So using “docker.host.internal” on the URI didn’t work (host machine is Ubuntu)</p>
  <p>Any suggestions ?? To solve this</p>
  
Tags: <docker><ubuntu><flask>
Title: Docker container host machine communication
UserDisplayName: user23619
ThreadText: # Docker container host machine communication
  
  (Asked by: username_0 on 2020-08-19)
  
  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
  
  **Tags:** docker, ubuntu, flask
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2020-08-25. Score: 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>
  
  ```
  
  > **Comments:**
  > **username_2**: Host network only works for Docker Swarm, not for regular Docker: https://docs.docker.com/network/  
  
  ---
  
  ### Answer by username_2 on 2020-08-25. Score: 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.
  
  
  ---
  
  ### Answer by username_3 on 2020-08-20. Score: -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
  
  ```
  
  > **Comments:**
  > **username_0**: 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  
  > **username_3**: 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.  
  > **username_3**: So the above workaround to provide a consistent address across all containers  
  
  ---
  
  