Overview
When developing with Docker or Podman, you often need containers to communicate with services running on your host machine (e.g., databases, APIs, development servers). This guide explains how to access host services from within containers across different platforms and runtimes.
Method 1: Using Special DNS Names (Recommended)
Container runtimes provide special DNS names that resolve to the host machine's IP address. This is the most portable and convenient method.
| Runtime | DNS Name |
|---|---|
| Docker | host.docker.internal |
| Podman | host.containers.internal |
Usage
From within your container, access host services using:
# Docker
http://host.docker.internal:[port]
# Podman
http://host.containers.internal:[port]
Example: If your host machine is running a web server on port 3000:
# Docker
curl http://host.docker.internal:3000
# Podman
curl http://host.containers.internal:3000
Platform Support
Docker:
- macOS: โ Works by default in Docker Desktop
- Windows: โ Works by default in Docker Desktop
- Linux: โ ๏ธ May require additional configuration (see below)
Podman:
- Linux: โ Works by default (Podman 4.1+)
- macOS: โ Works with Podman Desktop
- Windows: โ Works with Podman Desktop
Linux Configuration
On Linux, host.docker.internal might not resolve by default in older Docker versions. The recommended solution is to add an extra_hosts entry to your docker-compose.yml:
version: "3.8"
services:
your-service-name:
image: your-image
# ... other configurations
extra_hosts:
- "host.docker.internal:host-gateway"
What this does:
- The
host-gatewayvalue is a special Docker Compose keyword - It automatically resolves to the host machine's internal gateway IP address
- This makes
host.docker.internalwork consistently across all platforms
Alternative for docker run:
docker run --add-host=host.docker.internal:host-gateway your-image
Common Use Cases
Accessing a Development Database on Host
version: "3.8"
services:
web-app:
image: node:18
environment:
DATABASE_URL: "postgresql://user:[email protected]:5432/mydb"
extra_hosts:
- "host.docker.internal:host-gateway"
Connecting to a Host API Server
// Inside your containerized application
const API_BASE_URL = process.env.API_URL || 'http://host.docker.internal:8000';
fetch(`${API_BASE_URL}/api/data`)
.then(response => response.json())
.then(data => console.log(data));
Method 2: Using Host Networking Mode
An alternative approach is to run your container with --network=host, which makes the container share the host's network stack directly.
Usage
With docker run:
docker run --network=host your-image
With docker-compose.yml:
version: "3.8"
services:
your-service:
image: your-image
network_mode: host
How It Works
When using host networking mode:
- The container has the same network identity as the host
- You can connect to services using
http://localhost:[port]or the host's IP address - No need for
host.docker.internal - No port mapping required (ports are automatically exposed)
Example: If your host has a service on port 3000:
# Inside the container
curl http://localhost:3000
Trade-offs
Advantages:
- โ Simple configuration
- โ No DNS resolution needed
- โ Better network performance (no NAT overhead)
Disadvantages:
- โ Reduces network isolation between container and host
- โ Port conflicts: Container services will bind directly to host ports
- โ Less portable across different environments
- โ Not recommended for production deployments
- โ Does not work on Docker Desktop for Mac/Windows (host network mode is Linux-only)
When to Use Host Networking
Host networking mode is appropriate for:
- Quick local development testing
- Performance-critical applications where network latency matters
- Specific Linux-based deployment scenarios
Avoid host networking for:
- Production environments (use bridge networking with proper isolation)
- Multi-container applications that need network separation
- Cross-platform development (macOS/Windows won't work)
Method 3: Using the Docker Bridge Gateway IP (Linux-Specific)
On Linux, the Docker bridge network (typically docker0) has a default gateway IP that you can use to reach the host.
Finding the Gateway IP
# Method 1: Inspect docker0 interface
ip addr show docker0
# Look for: inet 172.17.0.1/16
# Method 2: Check from inside a container
docker run --rm alpine ip route | grep default
# Output: default via 172.17.0.1 dev eth0
Usage
The default Docker bridge gateway IP is usually 172.17.0.1, but this can vary:
# From inside the container
curl http://172.17.0.1:3000
Important: This IP address can change depending on your Docker configuration, so using host.docker.internal with host-gateway is more reliable.
Comparison Table
| Method | macOS/Windows | Linux | Recommended For | Network Isolation |
|---|---|---|---|---|
host.docker.internal (Docker) | โ Default | โ
With extra_hosts | Most scenarios | โ Good |
host.containers.internal (Podman) | โ Default | โ Default (4.1+) | Most scenarios | โ Good |
--network=host | โ Not supported | โ Works | Performance testing | โ None |
Bridge Gateway IP (172.17.0.1) | โ Not applicable | โ Works | Linux-only setups | โ Good |
Best Practices
- Use the appropriate DNS name for your runtime -
host.docker.internalfor Docker,host.containers.internalfor Podman - Add
extra_hostsin docker-compose.yml - Ensures consistent behavior on Linux systems - Avoid hard-coding IP addresses - Use environment variables for connection strings
- Reserve host networking for specific use cases - Don't use it as a default
- Test across platforms - If your team uses different operating systems, verify connectivity works everywhere
Troubleshooting
host.docker.internal or host.containers.internal Not Resolving
Symptom: curl: (6) Could not resolve host: host.docker.internal
Solution for Docker on Linux:
extra_hosts:
- "host.docker.internal:host-gateway"
Verify it works:
# Docker
docker run --rm --add-host=host.docker.internal:host-gateway alpine ping -c 3 host.docker.internal
# Podman (should work out of the box on 4.1+)
podman run --rm alpine ping -c 3 host.containers.internal
Solution for Podman (older versions):
If using Podman < 4.1, upgrade to a newer version or manually add the host:
podman run --add-host=host.containers.internal:host-gateway your-image
Connection Refused
Symptom: curl: (7) Failed to connect to host.docker.internal port 3000: Connection refused
Possible causes:
- Service is not running on the host
- Service is only listening on
127.0.0.1(loopback) - Firewall blocking container-to-host connections
Solution: Ensure your host service binds to 0.0.0.0 instead of 127.0.0.1:
// โ This won't work from containers
app.listen(3000, '127.0.0.1');
// โ
This will work
app.listen(3000, '0.0.0.0');
Firewall Issues (Linux)
If you're on Linux and connections are blocked:
# Check if firewall is blocking Docker
sudo iptables -L -n | grep DOCKER
# Temporarily disable firewall for testing
sudo systemctl stop firewalld # CentOS/RHEL
sudo ufw disable # Ubuntu/Debian
Security Considerations
- Avoid exposing sensitive services: Don't bind databases or admin interfaces to
0.0.0.0in production - Use network policies: In production, configure proper Docker network policies and firewalls
- Limit host networking usage: Only use
--network=hostwhen absolutely necessary - Monitor container network access: Log and audit connections from containers to host services
References
- Docker Documentation. (2025). Networking overview. Retrieved December 22, 2025, from https://docs.docker.com/network/
- Docker Documentation. (2025). Use host networking. Retrieved December 22, 2025, from https://docs.docker.com/network/host/
- Docker Compose Documentation. (2025). Compose file version 3 reference. Retrieved December 22, 2025, from https://docs.docker.com/compose/compose-file/compose-file-v3/