How to Get the IP Address of the Docker Host from Inside a Docker Container

When running services inside Docker, it’s common to need access to the Docker host machine. For example, you might want your container to connect to a database, API, or other service running directly on the host — not inside another container.

In such cases, you’ll need the IP address of the Docker host from inside the container.

This guide explains multiple ways to achieve this for different environments.


🔍 Why Would You Need the Host IP?

Some scenarios where you might need the host’s IP from a container include:

  • Testing applications in containers that connect to a local database running on your machine
  • Connecting to services hosted on the same physical machine without exposing them to the public network
  • Debugging networking issues between containers and the host

🛠 Methods to Get Docker Host IP

1. Using host.docker.internal (Preferred on Docker Desktop)

If you’re using Docker Desktop (Windows or macOS), Docker provides a special DNS name:

ping host.docker.internal

Inside your container, this will resolve to the host machine’s IP.

For Linux with newer Docker versions, you can enable it like this:

docker run --add-host=host.docker.internal:host-gateway my-container

This maps host.docker.internal to the host’s gateway IP.


2. Using the Default Gateway of the Container

On Linux, you can find the host’s IP by checking the container’s default route:

ip route | grep default

Example output:

default via 172.17.0.1 dev eth0

Here, 172.17.0.1 is the host’s IP address from the container’s perspective.


3. Passing Host IP via Environment Variable

If you already know your host’s IP, you can pass it directly:

docker run -e HOST_IP=$(hostname -I | awk '{print $1}') my-container

Inside the container, access it with:

echo $HOST_IP

This is useful when you have a static or known IP.


4. Using --network host (Linux only)

For Linux, running a container in host network mode means it shares the host’s network stack:

docker run --network host my-container

Now, localhost inside the container refers directly to the host.
⚠️ This mode bypasses network isolation — use with caution.


5. Using Docker Compose with extra_hosts

In a docker-compose.yml file:

version: '3.9'
services:
  app:
    image: my-container
    extra_hosts:
      - "host.docker.internal:host-gateway"

This gives your container a fixed hostname for the host IP.


📌 Summary

MethodWorks OnNotes
host.docker.internalWindows, macOS, Linux (new versions)Easiest and portable
Default Gateway (172.17.0.1)LinuxStandard bridge network
Env variableAllManual IP passing
--network hostLinuxNo network isolation
extra_hosts in ComposeAllGood for multi-container setups

🚀 Final Thoughts

The most portable method today is using host.docker.internal along with the host-gateway mapping, as it works across platforms and avoids hardcoding IP addresses. For Linux users in older setups, 172.17.0.1 (default gateway) is often the simplest solution.

By knowing how to access your host’s IP from inside a container, you can build more flexible and connected development environments without exposing services unnecessarily.

Sharing Is Caring:

Leave a Comment