Sub topic 12
Under the hood: NAT and packet flow
This chapter explains what the Linux kernel does when a container sends or receives a packet. It assumes you can run containers, know what an IP address, subnet, and port are, and have read the chapters on bridge networks and publishing ports. The commands below need a Linux host with root access. Docker Desktop runs them inside a hidden VM, so you won't see the same output on your host.
The pieces involved
Docker doesn't forward packets itself. It configures three kernel features and then steps aside:
- A Linux bridge, usually named
docker0for the default network. Each container's virtual interface is attached to it. - Routing and IP forwarding, which let the host pass packets between the bridge and the physical interface.
iptables(or nftables) rules, which perform NAT, the rewriting of source or destination addresses.
Two kinds of NAT matter. Masquerade (a form of source NAT) rewrites the source address of outbound traffic. Destination NAT (DNAT) rewrites the destination address of inbound traffic.
Outbound traffic: masquerade
A container on 172.17.0.0/16 has a private address that the outside network can't route back to. When it contacts an external server, Docker relies on a rule like this in the nat table:
sudo iptables -t nat -S POSTROUTING | grep MASQUERADEThe output contains something like -A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE. It means: for packets from the container subnet that are not leaving through the bridge itself, replace the source address with the host's address. The kernel remembers the translation in its connection tracking table, so replies are translated back and delivered to the right container.
Inbound traffic: DNAT for published ports
When you run docker run -d -p 8080:80 nginx:alpine, Docker adds a DNAT rule. You can see it in the DOCKER chain:
sudo iptables -t nat -S DOCKERExpect a line similar to -A DOCKER ! -i docker0 -p tcp -m tcp --dport 8080 -j DNAT --to-destination 172.17.0.2:80. A packet arriving on host port 8080 has its destination rewritten to the container's address and port 80. The packet is then routed to the bridge and delivered.
sudo conntrack -L 2>/dev/null | grep 8080The conntrack command, from the conntrack package, lists tracked connections. It shows both the original and translated tuples for each flow.
The full path
Where the rules live
Docker manages its own chains and inserts jumps to them from the built-in ones:
| Chain | Table | Purpose |
|---|---|---|
| `DOCKER` | nat | DNAT rules for published ports |
| `POSTROUTING` | nat | Masquerade for outbound traffic |
| `DOCKER-USER` | filter | Your own rules, evaluated before Docker's |
| `DOCKER-ISOLATION-*` | filter | Blocks traffic between different Docker networks |
| `FORWARD` | filter | Decides whether forwarded packets are allowed |
Common errors and gotchas
- Published ports bypass host firewall rules. Docker's DNAT happens in
PREROUTING, and the packet then goes throughFORWARD, notINPUT. Rules written forINPUT, including manyufwrules, don't apply. Put restrictions in theDOCKER-USERchain, or bind the port to127.0.0.1. - Disabled forwarding. If
net.ipv4.ip_forwardis0, containers can't reach outside networks. Docker normally enables it, but hardening scripts sometimes reset it. - Flushing iptables. Running
iptables -For reloading a firewall service removes Docker's rules. Restart the Docker daemon to recreate them. - Source addresses are lost. With the userland proxy or some NAT paths, the container may see the proxy or bridge gateway as the client address instead of the real one.
Exercise
A container on the default bridge can ping 8.8.8.8, but its replies reach it correctly even though 172.17.0.0/16 is not routable on the internet. Which kernel mechanisms make this work?
Show answer
The MASQUERADE rule in the nat table's POSTROUTING chain replaces the container's source address with the host's address on the way out. The connection tracking table records the translation, so the reply to the host address is translated back to the container's address and delivered through the bridge.
Exercise
You published a port with -p 5432:5432 and added a ufw deny 5432 rule, yet the database is still reachable from other machines. Why, and what are two ways to fix it?
Show answer
Docker's DNAT rewrites the packet before the filter decision, and the packet is then forwarded rather than delivered to the host, so it goes through the FORWARD chain and skips the INPUT rules that ufw manages. Fix it by publishing on the loopback interface (-p 127.0.0.1:5432:5432) or by adding a drop rule in the DOCKER-USER chain.