I'll also augment that as of Docker Engine 20.10, you can also use host.docker.internal
mapping on Linux (where it was previously only available on Docker Desktop). You do have to add an extra_hosts
mapping to your container definition though, such as:
docker run ... --add-host "host.docker.internal:host-gateway" ...
... or in your Compose YAML:
services:
gateway:
...
extra_hosts:
- "host.docker.internal:host-gateway"
....
With this, the hostname host.docker.internal
will resolve to your docker0
bridge IP on the host.