Docker 2-way localhost integration in Docker host networking mode

7 min read

In Docker host networking mode, containers share the same network stack as the host. This means that containers can access services running on the host using the host's localhost address (127.0.0.1).

To achieve 2-way localhost integration, both the host and container need to be configured to use the host's network stack.

To configure the host, add the following line to the Docker daemon configuration file (usually located at /etc/docker/daemon.json):

{
  "ip-forward": true
}

This allows containers to access the host's network stack.

To configure the container, use the following command when starting it:

docker run --network host <image>

This tells Docker to use the host's network stack instead of creating a separate network namespace for the container.

Once both the host and container are configured to use the same network stack, they can communicate with each other using the localhost address (127.0.0.1).

For example, if the host is running a web server on port 80, the container can access it using the URL http://localhost:80/. And if the container is running a web server on port 8080, the host can access it using the URL http://localhost:8080/.