Docker: Connect to host from container on Linux
Dominik Rüttiger 1 min read
If you’re coming from the Mac, you’re probably used to connecting to the host from inside a container with the address
host.docker.internal. On Linux, however, this does not work out of the box.
If you run the following command on a Linux machine, you will see that DNS resolution is not working:
docker run --rm alpine ping -c 1 host.docker.internal
To fix this, we need to set the DNS name manually:
docker run --add-host=host.docker.internal:host-gateway --rm alpine ping -c 1 host.docker.internal
host.docker.internal you could use any other name.
To achieve the same result in Docker Compose, use the
version: "3.9" services: test: image: alpine extra_hosts: - "host.docker.internal:host-gateway"
Test it with:
docker-compose run --rm test ping -c 1 host.docker.internal
- Overview of Docker container communication options
- Set specific ip address by creating a custom network
Twitter Hacker News Reddit