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.
Docker CLI
If you run the following command on a Linux machine, you will see that DNS resolution is not working:
To fix this, we need to set the DNS name manually:
Instead of host.docker.internal
you could use any other name.
Docker Compose
To achieve the same result in Docker Compose, use the extra_hosts
property.
Create a docker-compose.yml
file:
Test it with:
Further reading
- Overview of Docker container communication options
- Set specific ip address by creating a custom network