Skip to content

Lesson 4 of 9 · 27m

Container Networking Under the Hood

Follow packets through network namespaces, veth pairs, bridges, iptables or nftables, and published ports instead of treating Docker networking as magic.

Most container networking incidents are not exotic. They are NAT confusion, bridge confusion, DNS confusion, or a packet quietly bypassing the path where the firewall rule was expected. If you can follow one packet from process to veth pair to bridge to host forwarding, you can debug almost every routine Docker network problem without superstition.

Start with namespaces and interfaces

Every container with private networking has its own network namespace. Docker connects it to a host bridge using a virtual Ethernet pair:

docker run -d --name web -p 8080:80 nginx:1.27
pid=$(docker inspect -f '{{.State.Pid}}' web)
sudo nsenter -t "$pid" -n ip addr
sudo nsenter -t "$pid" -n ip route
ip -o link show type veth

Inside the namespace you typically see eth0 with an address such as 172.18.0.2/16. On the host you see a matching veth interface attached to br-... or docker0. They are two ends of one virtual cable.

Published ports are NAT and forwarding

-p 8080:80 is not opening a magic application tunnel. Docker programs host firewall rules so traffic arriving on host port 8080 is forwarded to container port 80:

sudo iptables -t nat -S DOCKER 2>/dev/null | head -30
sudo nft list ruleset 2>/dev/null | grep -A5 -i docker | head -30
ss -lntup | grep 8080

Depending on version and configuration, Docker may use iptables, nftables, or userspace proxy components. The operational point survives implementation drift: host port publication is host-level network state, and external firewalls see the host as the destination.

Bridge networks are scopes, not just subnets

User-defined bridges supply DNS-based service discovery and network separation:

docker network create backend
docker run -d --name db --network backend postgres:16
docker run --rm --network backend alpine:3.20 getent hosts db

Containers on separate bridge networks cannot resolve or reach each other merely because they share a host. That separation is useful, but it is not a complete security boundary if both can reach the same host services, cloud metadata endpoint, or permissive firewall path.

Follow the packet deliberately

A practical trace checklist is:

1. Is the process listening?             ss -lntup inside/container
2. Is the container interface up?        ip addr in namespace
3. Is there a route?                     ip route in namespace
4. Is the bridge forwarding?             bridge link / host sysctls
5. Is firewall state accepting it?       iptables/nft/conntrack
6. Is host port publication correct?     docker port and host ss

Do not jump from step one to restarting Docker. Restarting erases evidence and often leaves the same misconfiguration in place.

Host networking trades isolation for simplicity

--network host removes network namespace isolation. The process uses host interfaces, host ports, and host loopback directly. It can simplify performance-sensitive daemons, but it also means port collisions, no Docker DNS, no bridge isolation, and direct access to host-network services. Treat it as a conscious weakening of boundary, not a shortcut.

Field notes

  • EXPOSE in a Dockerfile is documentation and metadata; it does not publish a port.
  • Publishing 8080 without a host IP can bind broadly, including interfaces you did not expect. Prefer explicit 127.0.0.1:8080:80 for local-only services.
  • Docker modifies firewall state; host firewall managers that flush rules can break or bypass container networking.
  • Hairpin and localhost behavior differ between Linux routing, Docker proxy modes, and cloud NAT. Test the real path clients use.
  • Capture evidence with ip, ss, conntrack, and packet captures before restarting the daemon.

Was this lesson helpful?

Sign in to rate