Accessing Host Machine Services from Docker/Podman Containers

December 22, 2025

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.

Container runtimes provide special DNS names that resolve to the host machine's IP address. This is the most portable and convenient method.

RuntimeDNS Name
Dockerhost.docker.internal
Podmanhost.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-gateway value is a special Docker Compose keyword
  • It automatically resolves to the host machine's internal gateway IP address
  • This makes host.docker.internal work 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

MethodmacOS/WindowsLinuxRecommended ForNetwork Isolation
host.docker.internal (Docker)โœ… Defaultโœ… With extra_hostsMost scenariosโœ… Good
host.containers.internal (Podman)โœ… Defaultโœ… Default (4.1+)Most scenariosโœ… Good
--network=hostโŒ Not supportedโœ… WorksPerformance testingโŒ None
Bridge Gateway IP (172.17.0.1)โŒ Not applicableโœ… WorksLinux-only setupsโœ… Good

Best Practices

  1. Use the appropriate DNS name for your runtime - host.docker.internal for Docker, host.containers.internal for Podman
  2. Add extra_hosts in docker-compose.yml - Ensures consistent behavior on Linux systems
  3. Avoid hard-coding IP addresses - Use environment variables for connection strings
  4. Reserve host networking for specific use cases - Don't use it as a default
  5. 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:

  1. Service is not running on the host
  2. Service is only listening on 127.0.0.1 (loopback)
  3. 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.0 in production
  • Use network policies: In production, configure proper Docker network policies and firewalls
  • Limit host networking usage: Only use --network=host when absolutely necessary
  • Monitor container network access: Log and audit connections from containers to host services

References

  1. Docker Documentation. (2025). Networking overview. Retrieved December 22, 2025, from https://docs.docker.com/network/
  2. Docker Documentation. (2025). Use host networking. Retrieved December 22, 2025, from https://docs.docker.com/network/host/
  3. Docker Compose Documentation. (2025). Compose file version 3 reference. Retrieved December 22, 2025, from https://docs.docker.com/compose/compose-file/compose-file-v3/

ยฉ 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-12-22). Accessing Host Machine Services from Docker/Podman Containers. https://caiborui.com/blog/2025-12-22-en-docker-access-host-from-container

@misc{borui2025-ecd35aaa,
  author = {borui},
  title = {{Accessing Host Machine Services from Docker/Podman Containers}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-12-22-en-docker-access-host-from-container}
}