2

I have a docker container and I want to give it --network=host AND --network=postgres. I need to connect to the host network in order to use the host datadog server (UDP) and the postgres network for its database.

Trying to add both network results in docker: conflicting options: cannot attach both user-defined and non-user-defined network-modes..

Any idea what's the correct way to handle this?

TheUnreal
  • 23,434
  • 46
  • 157
  • 277
  • 1
    Can you take a look at these questions to see if they provide a solution to your specific case? 1. [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) and 2. [What is linux equivalent of `host.docker.internal`](https://stackoverflow.com/questions/48546124/what-is-linux-equivalent-of-host-docker-internal) – tgogos Jun 30 '22 at 08:53
  • Also, note that `host.docker.internal` won't work, if your Datadog service binds to `127.0.0.1:[port]` and not to `0.0.0.0:[port]` – tgogos Jun 30 '22 at 09:06
  • 1
    Thanks - worked! regards the first URL, it was too massive, I couldn't figure out what to do, still. the answer in this post + yoru comment helped me. – TheUnreal Jun 30 '22 at 12:46
  • Glad it worked! Yeah, its huge because it has all the "historical context" of how people were trying to solve it at the beginning only for `Linux`, then for `Docker for Windows / Mac` and finally with `host.docker.internal` when it became available... – tgogos Jun 30 '22 at 13:17

1 Answers1

3

You could use the the IP address of the host machine from within you container in order to send requests to the host machine.

A better solution, however, would be to use --add-host=host.docker.internal:host-gateway or

extra_hosts:
    - host.docker.internal:host-gateway

where host.docker.internal is then the host name of the host network inside your docker container. This name can of course be changed to your liking.

Credit: https://medium.com/@TimvanBaarsen/how-to-connect-to-the-docker-host-from-inside-a-docker-container-112b4c71bc66

  • This is covered in the canonical question [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach). – David Maze Jun 30 '22 at 10:24