How to fix dial tcp: lookup host.docker.internal: no such host in Docker

DockerINTERMEDIATEMEDIUMUpdated Dec 6, 2025· 456 views

This error occurs when a container tries to connect to host.docker.internal but the DNS name is not configured. On Linux, host.docker.internal requires explicit configuration unlike macOS and Windows where it works automatically.

Quick fix

Fixes dial tcp: lookup host.docker.internal: no such host

services:
  your-service:
    image: your-image
    extra_hosts:
      - "host.docker.internal:host-gateway"
  1. Add extra_hosts to docker-compose.yml
  2. Use --add-host flag with docker run
  3. Verify Docker version supports host-gateway
  4. Use Docker Desktop for Linux
docker — error
services:
your-service:
image: your-image
extra_hosts:
- "host.docker.internal:host-gateway"
dial tcp: lookup host.docker.internal: no such host

What this error means

When Docker containers need to communicate with services running on the host machine, they use the special DNS name `host.docker.internal`. This hostname is designed to resolve to an IP address that allows containers to reach the host network. On Docker Desktop for macOS and Windows, `host.docker.internal` is automatically configured and resolves correctly. However, on Linux systems running Docker Engine (not Docker Desktop), this DNS name is not available by default. The container's DNS resolver cannot find the hostname, resulting in the "no such host" error. This is a common source of confusion when developing locally with Docker. Code that works perfectly on macOS or Windows will fail on Linux CI/CD systems or production servers because the `host.docker.internal` resolution is not automatic. The solution involves explicitly mapping this hostname to the host's gateway IP address.

Typical symptoms

  • Container fails to connect to services running on the Docker host
  • Error message contains 'lookup host.docker.internal: no such host'
  • Application works on macOS/Windows but fails on Linux
  • Connection to localhost from within container fails
  • CI/CD pipeline fails when container tries to reach host services
  • Database connections using host.docker.internal time out or fail
  • API calls from container to host machine services return DNS errors

Common causes

  1. Running Docker Engine on Linux without explicit host.docker.internal configuration (most common)
  2. Docker version older than 20.10 which lacks host-gateway support
  3. Missing extra_hosts configuration in docker-compose.yml
  4. Missing --add-host flag when using docker run
  5. Docker Desktop for Linux not configured to enable host.docker.internal
  6. Custom DNS configuration in Docker daemon overriding default resolution
  7. Network mode incompatibility preventing host gateway access
  8. Firewall blocking connections from container to host network

How to fix "dial tcp: lookup host.docker.internal: no such host"

1Add extra_hosts to docker-compose.yml

The most common solution for Linux systems is to add the extra_hosts configuration to your docker-compose.yml file:

yaml
services:
  your-service:
    image: your-image
    extra_hosts:
      - "host.docker.internal:host-gateway"

The special value host-gateway tells Docker to replace it with the host's gateway IP address. This adds an entry to the container's /etc/hosts file.

Apply to all services that need host access:

yaml
services:
  app:
    image: node:18
    extra_hosts:
      - "host.docker.internal:host-gateway"

  worker:
    image: python:3.11
    extra_hosts:
      - "host.docker.internal:host-gateway"

After making this change, restart your containers:

bash
docker compose down
docker compose up -d
2Use --add-host flag with docker run

When running containers with docker run instead of Docker Compose, use the --add-host flag:

bash
docker run --add-host=host.docker.internal:host-gateway your-image

This achieves the same effect as the docker-compose.yml configuration.

Example with additional options:

bash
docker run -d \
  --name my-app \
  --add-host=host.docker.internal:host-gateway \
  -p 3000:3000 \
  my-app-image

Verify the host entry was added:

bash
docker exec my-app cat /etc/hosts

You should see a line like:

bash
172.17.0.1    host.docker.internal
3Verify Docker version supports host-gateway

The host-gateway special value requires Docker Engine version 20.10.0 or later (released December 2020).

Check your Docker version:

bash
docker --version

If you see "invalid IP address in add-host: host-gateway", you need to upgrade Docker.

Upgrade Docker on Ubuntu/Debian:

bash
# Remove old version
sudo apt remove docker docker-engine docker.io containerd runc

# Install latest Docker
curl -fsSL https://get.docker.com | sudo sh

# Verify version
docker --version

Upgrade Docker on CentOS/RHEL/Fedora:

bash
sudo yum remove docker docker-common docker-selinux docker-engine
curl -fsSL https://get.docker.com | sudo sh
docker --version
4Use Docker Desktop for Linux

Docker Desktop for Linux includes host.docker.internal support out of the box, similar to macOS and Windows.

Install Docker Desktop on Ubuntu:

bash
# Download the .deb package from Docker's website
# https://docs.docker.com/desktop/install/ubuntu/

sudo apt install ./docker-desktop-<version>-amd64.deb

Enable host.docker.internal in Docker Desktop:
1. Open Docker Desktop
2. Go to Settings (gear icon)
3. Navigate to Resources > Network
4. Ensure "Enable host.docker.internal" is checked
5. Click "Apply & Restart"

With Docker Desktop, host.docker.internal resolves automatically without extra configuration.

5Use host network mode as alternative

If you don't need network isolation, you can use host network mode where the container shares the host's network stack:

With docker-compose.yml:

yaml
services:
  your-service:
    image: your-image
    network_mode: host

With docker run:

bash
docker run --network host your-image

Important considerations:
- The container uses the host's IP address directly
- No port mapping needed (or possible) - ports bind directly to host
- Container can access localhost services on the host
- Reduces network isolation - use with caution in production
- May not work on macOS/Windows Docker Desktop (host networking is Linux-only)

This approach eliminates the need for host.docker.internal entirely because localhost in the container refers to the actual host.

6Manually specify the host IP address

If host-gateway is not available, you can manually specify the Docker bridge gateway IP:

Find the gateway IP:

bash
docker network inspect bridge --format '{{range .IPAM.Config}}{{.Gateway}}{{end}}'

This typically returns 172.17.0.1.

Use the IP in docker-compose.yml:

yaml
services:
  your-service:
    image: your-image
    extra_hosts:
      - "host.docker.internal:172.17.0.1"

Or with docker run:

bash
docker run --add-host=host.docker.internal:172.17.0.1 your-image

Note: The gateway IP can vary if you have multiple Docker networks. You may need to adjust it based on your network configuration.

7Configure firewall to allow container access

Even with correct DNS resolution, firewall rules may block connections from containers to the host.

Allow Docker bridge network on Ubuntu (ufw):

bash
# Allow from Docker bridge subnet
sudo ufw allow from 172.17.0.0/16

# Or more specifically, allow a port
sudo ufw allow from 172.17.0.0/16 to any port 5432

Allow on CentOS/RHEL (firewalld):

bash
# Add Docker zone
sudo firewall-cmd --permanent --zone=docker --add-source=172.17.0.0/16
sudo firewall-cmd --permanent --zone=docker --add-port=5432/tcp
sudo firewall-cmd --reload

Test connectivity from container:

bash
docker run --rm --add-host=host.docker.internal:host-gateway alpine \
  sh -c "apk add --no-cache curl && curl -v host.docker.internal:5432"

Replace 5432 with the port of the service you're trying to reach.

8Ensure host service binds to correct interface

Services on the host must listen on an interface accessible to containers, not just localhost.

Problem: Service only listens on 127.0.0.1

bash
# This only accepts local connections
postgres -h 127.0.0.1 -p 5432

Solution: Bind to 0.0.0.0 or the Docker bridge IP

bash
# This accepts connections from any interface
postgres -h 0.0.0.0 -p 5432

# Or bind to the Docker bridge specifically
postgres -h 172.17.0.1 -p 5432

For PostgreSQL (postgresql.conf):

bash
listen_addresses = '*'

For MySQL (my.cnf):

bash
bind-address = 0.0.0.0

For Node.js applications:

javascript
// Instead of
app.listen(3000, '127.0.0.1');
// Use
app.listen(3000, '0.0.0.0');

Security note: When binding to 0.0.0.0, ensure your firewall only allows connections from trusted sources.

Advanced notes

How host.docker.internal works internally:
On Docker Desktop (macOS/Windows), Docker runs inside a Linux VM. The host.docker.internal DNS name resolves to a special IP that routes traffic from the VM to the host machine. On native Linux, there's no VM layer, so this routing must be configured explicitly using the host gateway.

The host-gateway special value:
When you use host-gateway in --add-host or extra_hosts, Docker replaces it with the IP address of the default bridge gateway (typically 172.17.0.1). This IP is the host's interface on the Docker bridge network.

Cross-platform development tips:
For code that needs to work on all platforms:
1. Always include extra_hosts: ["host.docker.internal:host-gateway"] in docker-compose.yml
2. This is harmless on macOS/Windows and essential on Linux
3. Use environment variables for host references:

yaml
services:
  app:
    environment:
      - DB_HOST=host.docker.internal
    extra_hosts:
      - "host.docker.internal:host-gateway"

Troubleshooting DNS inside containers:

bash
# Check if hostname resolves
docker run --rm --add-host=host.docker.internal:host-gateway alpine nslookup host.docker.internal

# Check /etc/hosts entry
docker run --rm --add-host=host.docker.internal:host-gateway alpine cat /etc/hosts

# Test TCP connectivity
docker run --rm --add-host=host.docker.internal:host-gateway alpine nc -zv host.docker.internal 5432

Alternative: Use service names in compose:
If both services are in the same docker-compose file, use Docker's internal DNS instead of host.docker.internal:

yaml
services:
  app:
    depends_on:
      - db
    environment:
      - DB_HOST=db  # Use service name instead
  db:
    image: postgres

Docker Engine vs Docker Desktop on Linux:
- Docker Engine: Requires explicit host-gateway configuration
- Docker Desktop for Linux: Includes automatic host.docker.internal support but uses more system resources

Frequently asked questions

What does "dial tcp: lookup host.docker.internal: no such host" mean?
internal. This hostname is designed to resolve to an IP address that allows containers to reach the host network. internal is automatically configured and resolves correctly.
How do I fix "dial tcp: lookup host.docker.internal: no such host"?
This error occurs when a container tries to connect to host.docker.internal but the DNS name is not configured. On Linux, host.docker.internal requires explicit configuration unlike macOS and Windows where it works automatically.
What causes "dial tcp: lookup host.docker.internal: no such host"?
Running Docker Engine on Linux without explicit host.docker.internal configuration (most common) Docker version older than 20.10 which lacks host-gateway support Missing extra_hosts configuration in docker-compose.yml
Is the "dial tcp: lookup host.docker.internal: no such host" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading