Skip to main content

Docker Host Networking Explained

Networking in Docker can be confusing. One of the most powerful (and misunderstood) modes is Host Networking. This guide explains what it is, when to use it, and how to solve the common "how do I access localhost from my container?" problem.

What is Host Networking?

By default, Docker containers run in their own isolated network namespace (Bridge mode). They get their own IP address and port mapping is required to expose services.

Host Networking removes this isolation. The container shares the host's networking namespace directly.

  • No Port Mapping Needed: If a container listens on port 80, it is directly accessible on the host's port 80.
  • Performance: Slightly better performance as it skips the NAT (Network Address Translation) layer.
  • Limitations: Port conflicts are possible. You can't run two containers listening on port 80 in host mode on the same machine.

How to Use Host Networking

Docker CLI

Use the --network host flag:

docker run --network host nginx

Docker Compose

Set network_mode: host in your service definition:

services:
my-app:
image: my-app:latest
network_mode: host

Platform Differences (Important!)

Linux

Host networking works exactly as described. The container shares the host's network interface.

macOS and Windows (Docker Desktop)

Host networking does NOT work as expected. Because Docker on Mac/Windows runs inside a lightweight Linux VM, --network host attaches the container to the VM's network, not your physical Mac or Windows machine's network.

This means you cannot access the container on localhost just by using host mode, and the container cannot see your Mac/Windows services on localhost.

Accessing Host Services (host.docker.internal)

A very common requirement is connecting from a container to a database or API running on your local machine (outside Docker).

On macOS and Windows

Docker Desktop provides a special DNS name: host.docker.internal.

Inside your container, you can connect to http://host.docker.internal:3000 to reach a service running on port 3000 of your Mac/Windows host.

On Linux

host.docker.internal is not available by default on standard Docker installations on Linux.

The Fix: You must add it manually using extra_hosts in Docker Compose or --add-host in CLI.

Docker Compose:

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

Docker CLI:

docker run --add-host host.docker.internal:host-gateway my-app

With this configuration, host.docker.internal will resolve to the host's IP address on Linux, matching the behavior on Mac/Windows.

Summary

FeatureLinuxmacOS / Windows
--network hostWorks natively (shares host IP)Connects to Docker VM (limited use)
host.docker.internalNeeds extra_hosts configWorks out of the box

Networking Made Simple

Dealing with network bridges, port conflicts, and host gateways can be a headache.

Deploy on Hostim.dev

Hostim.dev simplifies networking. Your services can communicate easily, and we handle the ingress and SSL termination for you.