From inside of a Docker container, how do I connect to the localhost of the machine?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Inside a container, localhost means the container itself, not the host machine. To reach a service running on the host, you need a host-accessible address or a networking mode that deliberately shares the host network.
Why localhost does not mean the host
Containers run in their own network namespace. That means:
- '
127.0.0.1inside the container points back to the container' - the host machine is a different network peer
This is the core reason a command such as:
fails inside the container even though the service is listening on the host.
The easiest option on Docker Desktop
On Docker Desktop for macOS and Windows, Docker provides a special hostname:
That hostname resolves to the host machine from inside the container. It is the most convenient solution on those platforms.
You can test it with:
If the service is listening on the host and is reachable, the container can use that hostname directly.
Linux options
On Linux, the situation is a little different. A common modern approach is to add the host gateway mapping explicitly:
That creates the host.docker.internal name inside the container and points it at the Docker host gateway.
Another practical option is to use the bridge gateway address directly, which is often something like 172.17.0.1:
The exact address depends on the Docker network configuration, so verify it instead of assuming.
Using host networking
On Linux, you can also run the container with host networking:
In that mode, localhost inside the container refers to the host network namespace, so host services on localhost become reachable directly.
This is convenient for local debugging, but it reduces isolation. It is usually not the best default for general container deployments.
Make sure the host service is actually reachable
Even with the right address, the host service must be listening on an interface the container can reach. A service bound only to 127.0.0.1 may behave differently from one bound to 0.0.0.0, depending on the networking mode you use.
Good checks include:
Verify both:
- which address the host service is bound to
- whether the container is on the expected network path
Common Pitfalls
The most common mistake is assuming localhost always means "my machine." In container networking, it means "this container."
Another issue is relying on host.docker.internal everywhere without checking platform behavior. It is easy on Docker Desktop and still needs explicit help on many Linux setups.
People also forget firewall rules and service bind addresses. If the host service is not listening in a reachable way, the correct hostname alone will not save you.
Finally, use host networking intentionally. It is useful for debugging and some local workflows, but it changes the isolation model of the container.
Summary
- '
localhostinside a container refers to the container, not the host machine.' - On Docker Desktop, use
host.docker.internalto reach host services. - On Linux,
--add-host=host.docker.internal:host-gatewayis a practical modern option. - Host networking can make
localhostwork directly on Linux, but it reduces isolation. - Always verify the host service’s bind address and port before debugging Docker networking further.

