Fixes

Fix

Docker container can't reach the internet

The container's packets leave with its private 172.17 address, so replies cannot come back. The host needs a MASQUERADE rule for the container network.

Likely causes

  1. No MASQUERADE (NAT) rule for the container network on the host.
  2. The container's veth is not on the docker0 bridge, or the container has no default route via 172.17.0.1.
  3. Forwarding is off on the host (net.ipv4.ip_forward = 0).

Diagnose

Run these in order. The output is what each printed on a lab's real Linux machines.

box:~# ip netns exec shop ip -4 -br addr
Inside a container: one card with a private address, 172.17.0.2/16.
lo               UNKNOWN        127.0.0.1/8 
eth0@if7         UP             172.17.0.2/16 
box:~# ip netns exec shop ip route
Its default route goes to 172.17.0.1, the host's bridge docker0.
default via 172.17.0.1 dev eth0 
172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.2 
box:~# ip -br link show master docker0
On the host: each container's cable ends in a veth plugged into docker0.
veth-shop@if5    UP             a2:92:84:b8:1b:08 <BROADCAST,MULTICAST,UP,LOWER_UP> 
veth-blog@if5    UP             82:e1:8a:9c:b4:cf <BROADCAST,MULTICAST,UP,LOWER_UP> 

Fix

box:~# ip netns exec shop curl -sS -m 3 http://198.51.100.10/
With the host's masquerade rule, the server sees the host's address and its answer comes back.
Package mirror: 2,318 packages. Your request came from 203.0.113.10
box:~# conntrack -L -s 172.17.0.2
conntrack shows the rewrite: the answer goes to the host, which hands it on to the container.
tcp      6 118 TIME_WAIT src=172.17.0.2 dst=198.51.100.10 sport=41938 dport=80 src=198.51.100.10 dst=203.0.113.10 sport=80 dport=41938 [ASSURED] mark=0 use=1
conntrack v1.4.8 (conntrack-tools): 1 flow entries have been shown.
iptables -t nat -A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE
When the rule is missing, add it: anything from the container network that leaves by another card takes the host's address.

The fix the lab's challenge "The shop can't fetch its updates" checks.

Break it and fix it yourself

Each lab is a small network of real Linux machines in your browser, where this fault happens.

Open Container networks: give every container a network of its own

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab