From Inside a Docker Container, How Do I Connect to the Host’s localhost?

When running applications in Docker containers, it’s common to need access to services running on the host machine, such as a local database, API, or development server. But by default, localhost inside a Docker container refers to the container itself, not the host.

So, how can you connect to the host’s localhost from inside a Docker container? Let’s explore the most effective solutions based on your operating system and Docker setup.


๐Ÿง  Understanding the Problem

  • Inside a container, localhost (127.0.0.1) refers to the containerโ€™s own loopback interface.
  • If your host is running a service on localhost:5000, a container cannot access it via localhost:5000.
  • Instead, the container needs a way to reach the host network.

โœ… Solution 1: Use host.docker.internal (Docker for Windows & Mac)

If you are using Docker Desktop on Windows or macOS, Docker provides a special DNS name: host.docker.internal.

How to Use:

curl http://host.docker.internal:5000

Or in a Dockerfile or script inside the container:

ping host.docker.internal

๐Ÿ›  Supported on:

  • Docker Desktop for Windows
  • Docker Desktop for Mac

๐Ÿ’ก Tip: This does not work natively on Linux, unless explicitly configured.


โœ… Solution 2: Linux โ€“ Use --network="host" Mode

On Linux, the easiest way to access the host’s localhost is to run the container in host network mode:

docker run --network="host" your-image

This makes the container share the hostโ€™s network stack, so localhost inside the container is the same as localhost on the host.

โš ๏ธ Note:

  • Only works on Linux.
  • Not compatible with Docker Desktop for Windows/Mac.
  • Can pose security concerns as there’s no network isolation.

โœ… Solution 3: Find Host IP from Inside the Container

You can connect to the host machineโ€™s IP address on the Docker bridge network. The default gateway of the container is often the host machineโ€™s bridge IP.

Step-by-Step:

  1. Run this inside your container: ip route | grep default
  2. Output might look like: default via 172.17.0.1 dev eth0
  3. Then, connect to the host using that IP: curl http://172.17.0.1:5000

๐Ÿ”„ Alternative:

You can also hardcode the host IP (e.g., 172.17.0.1), but this can vary by system and network setup.


โœ… Solution 4: Use Docker Compose with Extra Host Mapping

If you’re using Docker Compose, you can manually map a hostname to the host machine:

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

๐Ÿ›  Requires:

  • Docker v20.10.0+ (for host-gateway support on Linux)

Now your container can access the host using:

curl http://host.docker.internal:5000

๐Ÿงช Example Use Case: Accessing a Database on Host

If your host machine runs a local PostgreSQL database on port 5432, and you want to access it from a container:

On Windows/Mac:

psql -h host.docker.internal -p 5432 -U postgres

On Linux (with host networking):

docker run --network host postgres psql -h 127.0.0.1 -U postgres

โœ… Summary

MethodOS / PlatformUse Case
host.docker.internalWindows / macOSMost common & simple method
--network="host"Linux onlyFull access to host network
Use 172.17.0.1Linux (Docker bridge)When bridge IP is known
extra_hosts with ComposeLinux + Docker 20.10+Flexible and Docker-native

๐Ÿš€ Final Tip

Always make sure your host firewall or services (e.g., MySQL, PostgreSQL) are listening on 0.0.0.0 or specific interfaces, not just 127.0.0.1. Otherwise, the container wonโ€™t be able to reach them even if the address is correct.

Sharing Is Caring:

Leave a Comment