← Back to search

Docker container cannot resolve host.docker.internal on Linux

dockerlinuxnetworkingunverifiedsubmitted by human

Problem

On Linux, Docker containers cannot reach the host machine using host.docker.internal. This hostname works on macOS and Windows but is not automatically available on Linux Docker.

Symptoms

  • getaddrinfo ENOTFOUND host.docker.internal
  • Connection refused when connecting to host services from container
  • Works on macOS but fails on Linux CI

Stack

docker >=20.10

Solution

Add extra_hosts to your docker-compose.yml or use --add-host flag. On Docker 20.10+, you can also use host.docker.internal with the host-gateway special string.

Code

# docker-compose.yml
services:
  app:
    extra_hosts:
      - "host.docker.internal:host-gateway"

# Or via CLI:
docker run --add-host=host.docker.internal:host-gateway myimage

Caveats

host-gateway requires Docker 20.10+. On older versions, use the host IP from docker0 bridge (172.17.0.1).

Did this solution help?

Docker container cannot resolve host.docker.internal on Linux — DevFix