On Mac and Windows it is possible to use docker.for.mac.host.internal
(replaces docker.for.mac.localhost
) and docker.for.win.host.internal
(replaces docker.for.win.localhost
) host.docker.internal
(Docker 18.03+) inside container.
Is there one for Linux that will work out of the box without passing env variables or extracting it using various cli commands.
Depends what you're trying to do. If you're running with --net=host
, localhost
should work fine. If you're using default networking, use the static IP 172.17.0.1
. I suspect neither will behave quite the same as those domains.
One solution is to use a special container which redirects traffic to the host. You can find such a container here: https://github.com/qoomon/docker-host. The idea is to grab the default route from within the container and install that as a NAT gateway for incoming connections.
Below an imaginary example usage:
docker-host:
image: qoomon/docker-host
cap_add: [ 'NET_ADMIN', 'NET_RAW' ]
restart: on-failure
environment:
- PORTS=999
some-service:
image: ...
environment:
SERVER_URL: "http://docker-host:999"
command: ...
depends_on:
- docker-host
For linux there isn't a default DNS name for the host machine. This can be verified by running the command:
docker run -it alpine cat /etc/hosts
This feature has been requested, however wasn't implemented. You can check this issue. As discussed you can use the following command to find the IP of the host from the container.
netstat -nr | grep '^0\.0\.0\.0' | awk '{print $2}'
Alternatively, you can provide the host ip to the run command via docker run --add-host dockerHost:<ip-address> ...