Accessing Host Services from Inside Podman Compose Containers

Accessing Host Services from Inside Podman Compose Containers

Overview

When running containers with Podman Compose, software inside the container may need to access services running on the host machine (e.g., a database, API, or development server). However, localhost inside a container refers to the container itself—not the host.

This guide explains how to correctly route traffic from a container to the host.

ℹ️ Note: This method also works with Docker Compose, as long as the container runtime supports host.docker.internal or equivalent DNS resolution.


Podman provides a special internal DNS name to reference the host from inside a container:


host.containers.internal

This works with Podman 3.4+ and is the most reliable and secure method.

Example

If your host is running a service on localhost:8000, you can access it inside the container as:

http://host.containers.internal:8000

No additional configuration is required.


🔧 Using Podman Compose

1. Run Your Host Service

Ensure the service you want to access is running on the host (e.g., localhost:1234).

2. Configure Your Container

Update your application or environment variables to reference the special host address. Example docker-compose.yml (compatible with Podman Compose):

services:
  app:
    image: your-image
    environment:
      - API_URL=http://host.containers.internal:1234

3. Start Podman Compose

podman-compose up

Your containerized application will now connect to the host service via the internal alias.


🛠 Troubleshooting

  • If host.containers.internal doesn't resolve:

    • Ensure you're using Podman 3.4+.
    • Test DNS inside the container: ping host.containers.internal
    • Manually add a host entry via volume mount if needed.

⚠️ Alternative: Host Network Mode (Linux Only)

Use network_mode: host to fully expose the host network to the container:

services:
  app:
    image: your-image
    network_mode: host

Warning: This disables network isolation and may cause port conflicts. Only use when necessary.


✅ Summary

To allow a container to access services on the host:

  • Use host.containers.internal:<port> inside the container.
  • Works with Podman Compose and Docker Compose (on compatible systems).
  • Avoid network_mode: host unless isolation is not required.
↑ Back to the top