Chapter 2 · Lesson 16 of 20
Docker

Docker bridge vs host networking: which to use

A bridge network gives each container its own network namespace behind NAT, while host mode makes the container share the host's network stack directly.

By the end you will

Choose between bridge, user-defined bridge, host and none networking, and explain what happens to -p in each.

Time
12 min
Level
Intermediate
Check
4 questions
You need
  • Docker Engine installed on a Linux server
  • Comfort with docker run and port publishing

Docker offers several network modes, and the two you meet most are bridge and host. A bridge network puts each container in its own network namespace with a private IP address, and traffic reaches it through port publishing. Host mode skips that isolation: the container uses the server’s own network stack, so -p is ignored. Use bridge by default and host only when you have a specific reason.

How it works#

When the Docker daemon starts, it creates a Linux bridge called docker0 and a default network named bridge. A container attached to a bridge network gets a virtual ethernet pair: one end inside the container, one end plugged into the bridge. Docker assigns a private address from a pool, commonly in the 172.17.0.0/16 range for the default bridge, and uses NAT rules so that traffic to a published host port is forwarded to the container.

In host mode there is no virtual pair and no NAT. The container process runs in the host’s network namespace, sees the host’s interfaces and binds ports on them directly. For the wider picture of networks, read Docker networks explained.

Default bridge vs user-defined bridge#

Both are bridges, but they behave differently. List what you have:

bash
docker network ls

You will see bridge, host and none created by Docker, plus any networks you or Compose added.

The default bridge named bridge is a legacy setup. Containers on it can reach each other by IP address only. There is no name resolution between them, and every container joins it unless you say otherwise.

A user-defined bridge adds an embedded DNS server, so containers find each other by name, and you can attach and detach containers while they run. Create one and use it:

bash
docker network create appnet
docker run -d --name db --network appnet postgres:16
docker run -d --name web --network appnet -p 8080:80 nginx

Here web can reach the database at the hostname db. Compose creates a user-defined bridge per project automatically, which is why services in the same Compose file can call each other by service name. The mechanics of that are in DNS inside Docker.

Host mode#

Start a container in host mode:

bash
docker run -d --name web --network host nginx

If nginx listens on port 80 inside, it now listens on port 80 of the server, with no -p needed. If you add -p 8080:80, Docker prints a warning that published ports are discarded in host mode and ignores the flag. The practical effects:

  • No NAT hop, which can matter for workloads that open many connections or need very low overhead.
  • The container can see and bind every host interface, including addresses you may not want it to touch.
  • Two containers cannot both listen on port 80, because they share the host’s port space.
  • You lose the isolation that bridge networks give, and container-to-container firewalling by network is gone.

Check what is listening on the host with ss -ltn. In host mode the app’s port appears there without any Docker proxy involved. If something else already holds the port, the container fails to bind. See find what is using a port.

Host mode on Docker Desktop#

On a Linux server, host mode means the real server. On Docker Desktop for macOS and Windows, Docker Engine runs inside a lightweight VM. Historically, host mode there meant the VM’s network, not your laptop’s, so a service started with --network host was not reachable at localhost on your machine. Newer Docker Desktop versions added an opt-in setting for host networking, so check the Docker Desktop documentation for your version before relying on it. For anything you plan to deploy, test on Linux, because production behavior is what counts.

The none network#

--network none gives the container only a loopback interface. It cannot reach the internet or other containers, and nothing can reach it.

bash
docker run --rm --network none alpine ip addr

This prints only the loopback device. Use none for batch jobs that process local files and must never make a network call, or as a base when you attach a custom network setup yourself.

When each fits#

  • Default pattern for web apps and databases: a user-defined bridge, with only the reverse proxy publishing ports.
  • Host mode: network monitoring tools, a proxy that must see real source addresses with no NAT in between, or software that needs many dynamic ports. Accept the loss of isolation.
  • None: sandboxed processing with no network need.
  • Default bridge: quick throwaway containers where name lookup is not needed.

Check it worked#

Inspect which network a container joined and what IP it has:

bash
docker inspect web --format '{{json .NetworkSettings.Networks}}'

For a bridge container you see the network name and an IPAddress. For a host-mode container the network is host and the IP address field is empty, because the container has no address of its own.

Confirm that -p was dropped in host mode with docker port web, which prints nothing for that container, then confirm the real listener with ss -ltn.

Edge cases and gotchas#

  • Ignored -p: Docker warns that published ports are discarded in host mode. If a tutorial adds both -p and --network host, the -p part is dead weight.
  • Port clashes: Two host-mode containers, or a host-mode container and a host service such as nginx, cannot share a port. The second one fails to bind.
  • Firewall exposure: A host-mode service is exposed exactly as a host service would be. Your UFW rules do apply to it, unlike bridge-published ports, which Docker routes around the usual rules.
  • Name resolution: Moving a stack from a user-defined bridge to host mode breaks service-name lookups. Use 127.0.0.1 and real ports instead.
  • Compose syntax: In Compose, host mode is network_mode: host on a service, and it cannot be combined with ports or custom networks on that service.
  • Docker Desktop: Host mode may refer to the VM, so results on a laptop can differ from a Linux server.

Troubleshooting#

SymptomLikely causeFix
Warning that published ports are discarded-p used with --network hostRemove -p, or switch to a bridge network
Container exits with address already in useHost mode and another process holds the portFind the owner with ss -ltnp, stop it or change the app port
Containers cannot reach each other by nameBoth are on the default bridgeCreate a user-defined network and attach both
Host-mode service unreachable on a laptopDocker Desktop VM networkingTest on a Linux host, or use bridge with -p
Container has no internetStarted with --network noneRecreate it on a bridge network

Quick reference#

bash
docker network ls
docker network create appnet
docker run -d --network appnet --name web nginx
docker run -d --network host nginx
docker run --rm --network none alpine ip addr
docker inspect web --format '{{json .NetworkSettings.Networks}}'

Next#

Continue with what 0.0.0.0 means when publishing Docker ports.

Check what you learned4 questions
1You run docker run --network host -p 8080:80 nginx. What happens to the -p flag?
2Which network type gives containers name resolution between each other?
3What does --network none give a container?
4Why does host mode cause port conflicts that bridge mode does not?

Frequently asked questions

What is the difference between bridge and host network in Docker?

Bridge gives each container a private IP behind NAT and you publish ports with -p. Host mode removes that isolation, so the container uses the host's network interfaces and ports directly.

Does -p work with --network host?

No. Docker ignores published ports in host mode because the container is already on the host's network. Whatever port the app listens on is the port exposed on the host.

Should I use the default bridge or a user-defined bridge?

Use a user-defined bridge, which Compose creates for you. It gives containers DNS names for each other and better isolation than the legacy default bridge.

Does host networking work on Docker Desktop?

Docker Desktop runs containers inside a VM, so host mode refers to that VM, not your laptop, unless a newer Docker Desktop feature for host networking is enabled. On a plain Linux server it behaves as described.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios