Day 11: Container Networking
Goal: You recognize that a container network is the same thing as your training ground – namespace, veth, bridge, NAT –, look into a container without tools using
nsenter, read port publishing as a DNAT rule, understand Docker’s DNS and find two classics: a service that is reachable only inside the container, and a host whose forwarding Docker has shut off · Duration: approx. 90 min · Prerequisite: Day 10, internet access for downloading two small images (nginx, busybox, about 25 MB together)
Briefing
- Since Day 0 you have been building networks from namespaces, veth
cables and bridges. Docker does exactly the same, just automatically:
every container is a network namespace, its
eth0is one end of a veth pair, the other end sits in the bridgedocker0, and traffic leaves via masquerade. - The everyday problem: containers usually ship no
tools – no
ip, noss, nocurl. Withnsenteryou take the VM’s tools along into the container’s network. - Kubernetes pods work the same way (Days 12 and 13). What you practice on Docker today still applies there.
Drill 1: A Container from Inside and Outside
# on netz
# Docker from the Ubuntu packages
sudo apt-get install -y docker.io
# nginx as a container, publish the container's port 80 on port 8088 of the VM
# run -d start in the background
# --name web1 name of the container
# -p 8088:80 port publishing: VM port 8088 → container port 80
# nginx:1.27-alpine image (small, without network tools)
sudo docker run -d --name web1 -p 8088:80 nginx:1.27-alpine
# Which address does it have, and what is the process ID of its main process?
# inspect -f '…' output individual fields (Go template)
sudo docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}} {{.State.Pid}}' web1Expected (the long ID after run and the
PID differ):
3f1c…
172.17.0.2 57389
The image has no ip and no ss. Instead, you
enter the container’s network namespace with the VM’s programs:
# on netz
P=$(sudo docker inspect -f '{{.State.Pid}}' web1)
# nsenter -t <PID> enter the namespaces of this process
# -n only the network namespace (file system and programs stay those of the VM)
sudo nsenter -t $P -n ip -br addr
sudo nsenter -t $P -n ip route
sudo nsenter -t $P -n ss -ltnExpected:
lo UNKNOWN 127.0.0.1/8 ::1/128
eth0@if37 UP 172.17.0.2/16
default via 172.17.0.1 dev eth0
172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.2
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 511 0.0.0.0:80 0.0.0.0:*
LISTEN 0 511 [::]:80 [::]:*
This is Day 0 in different packaging: one address, a default route to
the “router” 172.17.0.1, a service on port 80.
eth0@if37 tells you where the other end of the cable sits:
at interface number 37 – on the VM.
# on netz
# interface number 37 on the VM
# ip -o link one line per interface, starting with the number
ip -o link | grep '^37:' | awk '{print $1, $2}'
# ports of the bridge docker0, and its address
ip -br link show master docker0
ip -br addr show docker0Expected (name of the veth and number differ):
37: veth1234af2@if2:
veth1234af2@if2 UP 6e:3f:21:3b:cb:62 <BROADCAST,MULTICAST,UP,LOWER_UP>
docker0 UP 172.17.0.1/16 fe80::64e2:f1ff:fe31:3be8/64
| Training ground (Day 0) | Docker |
|---|---|
namespace web |
network namespace of the container (reachable via the PID) |
veth b-web ↔︎ eth0 |
veth veth1234af2 ↔︎ eth0 in the
container |
bridge br-b |
bridge docker0 |
router 10.2.0.1 |
172.17.0.1 on docker0, the VM itself
routes |
| masquerade at the router | masquerade in table ip nat, chain
POSTROUTING |
Save your progress? Sign up for free
Drill 2: Port Publishing Is DNAT
# on netz
# from the VM itself and via its address in the home network
curl -s -o /dev/null -w '127.0.0.1:8088 %{http_code}\n' http://127.0.0.1:8088/
curl -s -o /dev/null -w '172.16.0.123:8088 %{http_code}\n' http://172.16.0.123:8088/
# What did Docker set up for this?
sudo nft list chain ip nat DOCKER
sudo ss -ltnp 'sport = :8088'Expected (counters and PIDs differ; depending on the
version, table ip nat is preceded by the line
# Warning: table ip nat is managed by iptables-nft, do not touch!
– Docker manages this table):
127.0.0.1:8088 200
172.16.0.123:8088 200
table ip nat {
chain DOCKER {
iifname != "docker0" tcp dport 8088 counter packets 1 bytes 60 dnat to 172.17.0.2:80
}
}
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 4096 0.0.0.0:8088 0.0.0.0:* users:(("docker-proxy",pid=57413,fd=7))
LISTEN 0 4096 [::]:8088 [::]:* users:(("docker-proxy",pid=57419,fd=7))
Two mechanisms work together: the DNAT rule rewrites
packets from outside to 172.17.0.2:80 (Day 9). For
connections from the VM itself to 127.0.0.1 there is
additionally docker-proxy, a small forwarding process. That
is why ss on the VM shows a “listening” port 8088, even
though nginx in the container listens on 80.
An important consequence: -p 8088:80 means
0.0.0.0:8088 – the container is reachable on
all addresses of the VM, including from the home
network. Docker inserts its rules ahead of a host firewall such as
ufw; a ufw deny 8088 therefore doesn’t help.
If you want to publish a port only locally, write
-p 127.0.0.1:8088:80.
Save your progress? Sign up for free
Drill 3: Names Between Containers
# on netz
# A dedicated network instead of the default bridge
# network create shopnet new bridge with its own address range
sudo docker network create shopnet
sudo docker run -d --name api --network shopnet nginx:1.27-alpine
# A throwaway container in the same network calls the other one by name
# run --rm delete after it ends
# busybox:1.37 wget -qO- -T3 http://api/ small HTTP request, output to the screen, 3 s timeout
sudo docker run --rm --network shopnet busybox:1.37 wget -qO- -T3 http://api/ | grep -o '<title>.*</title>'
# The same in the default network bridge, with the name web1
sudo docker run --rm busybox:1.37 wget -qO- -T3 http://web1/Expected (the first time, Docker downloads busybox and reports it):
<title>Welcome to nginx!</title>
wget: bad address 'web1'
In a user-created network, every container knows the others by name;
in the default network bridge it doesn’t. The reason is in
the container’s resolv.conf and in its sockets:
# on netz
# grep -v '^#' leave out comment lines
sudo docker run --rm --network shopnet busybox:1.37 cat /etc/resolv.conf | grep -v '^#'
P=$(sudo docker inspect -f '{{.State.Pid}}' api)
# ss -lun listening UDP sockets
sudo nsenter -t $P -n ss -lunExpected (ports and search domain differ):
nameserver 127.0.0.11
search localdomain
options edns0 trust-ad ndots:0
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
UNCONN 0 0 127.0.0.11:40337 0.0.0.0:*
127.0.0.11 is Docker’s built-in DNS server, which exists
only in user-created networks. It answers container names itself and
forwards everything else to the VM’s name servers. Docker redirects
packets to 127.0.0.11:53 to the random port via a NAT rule
in the container – that is why it doesn’t say 53. In Kubernetes, CoreDNS
takes on this role (Day 13).
Save your progress? Sign up for free
Exercise
A developer starts a small web server in a container and publishes it:
# on netz
# httpd -f -p 127.0.0.1:8000 busybox web server in the foreground, only on 127.0.0.1:8000
sudo docker run -d --name klein -p 8089:8000 busybox:1.37 httpd -f -p 127.0.0.1:8000 -h /tmp“It works inside the container, but not from outside.” Test from the
VM and via the VM address, find the cause with nsenter and
fix it.
Solution
# on netz
curl -sS -m3 http://127.0.0.1:8089/
curl -sS -m3 http://172.16.0.123:8089/
P=$(sudo docker inspect -f '{{.State.Pid}}' klein)
sudo nsenter -t $P -n ss -ltnExpected:
curl: (56) Recv failure: Connection reset by peer
curl: (7) Failed to connect to 172.16.0.123 port 8089 after 0 ms: Couldn't connect to server
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 9 127.0.0.1:8000 0.0.0.0:*
Day 5 inside a container: the service listens on
127.0.0.1 – the loopback of the container.
The DNAT rule, however, sends the packets to
172.17.0.x:8000, and nobody listens there. Depending on the
path, this looks different: via the VM address, the container answers
with a reset (Couldn't connect); via
127.0.0.1, docker-proxy accepts first and then
aborts (Connection reset by peer). Fix: listen on all
addresses.
# on netz
# rm -f stop and delete the container
sudo docker rm -f klein
# sh -c '…' create a small start page, then start the web server on all addresses (-p 8000)
sudo docker run -d --name klein -p 8089:8000 busybox:1.37 sh -c 'echo hallo aus dem container > /tmp/index.html; httpd -f -p 8000 -h /tmp'
curl -s http://172.16.0.123:8089/Expected:
hallo aus dem container
Many development servers (Flask, Vite, rails s,
npm run dev) listen on 127.0.0.1 by default.
In a container you therefore almost always need
--host 0.0.0.0 or an equivalent setting.
Save your progress? Sign up for free
Challenge
Simulate what happens after a VM reboot: Docker starts at boot, before you rebuild the training ground from Day 0 – that is, with forwarding switched off.
# on netz
sudo sysctl -w net.ipv4.ip_forward=0
# Restart the Docker service – this also stops all containers (they stay stopped)
sudo systemctl restart dockerThe ticket: “Since the reboot, the client can no longer reach the internet. Everything internal works.” Check forwarding, use counters to find where the packets end up, and fix it so that Docker doesn’t break it again at the next restart.
Solution
# on netz
sudo ip netns exec client curl -sS -m5 -o /dev/null -w '%{http_code}\n' https://example.com/
sudo ip netns exec client curl -s http://web.firma.lab/
# Forwarding is on – Docker switched it back on itself
sysctl net.ipv4.ip_forward
# Who drops forwarded packets?
# -L FORWARD -v -n chain FORWARD with counters, addresses as numbers
sudo iptables -L FORWARD -v -n | head -4Expected (the counters differ):
curl: (28) Connection timed out after 5001 milliseconds
000
hallo von web
net.ipv4.ip_forward = 1
Chain FORWARD (policy DROP 6 packets, 360 bytes)
pkts bytes target prot opt in out source destination
6 360 DOCKER-USER 0 -- * * 0.0.0.0/0 0.0.0.0/0
6 360 DOCKER-FORWARD 0 -- * * 0.0.0.0/0 0.0.0.0/0
If Docker starts and finds forwarding switched off, it switches it on
– and, as a safeguard, sets the policy of the FORWARD chain to
DROP. From then on, everything that doesn’t belong to
Docker is no longer forwarded on the VM: here the path from the router
(up0) into the home network (eth0). Internal
traffic keeps working because it doesn’t cross the VM at all but stays
in the namespace router.
The obvious attempt – your own nft table with accept –
doesn’t help:
# on netz
# priority -10 evaluate before Docker's chain
sudo nft add table inet eigene
sudo nft add chain inet eigene weiter '{ type filter hook forward priority -10; policy accept; }'
sudo nft add rule inet eigene weiter iifname up0 counter accept
sudo ip netns exec client curl -sS -m5 -o /dev/null -w '%{http_code}\n' https://example.com/
sudo nft list chain inet eigene weiter | grep counter
sudo nft delete table inet eigeneExpected:
curl: (28) Connection timed out after 5001 milliseconds
000
iifname "up0" counter packets 2 bytes 136 accept
Your own rule matches (counter 2) and says accept –
still, nothing gets through. In nftables a packet has to survive
every base chain on the same hook; an
accept only ends its own chain, a drop
anywhere is final. The right place is the chain Docker provides exactly
for this purpose and never overwrites: DOCKER-USER.
# on netz
# -I DOCKER-USER insert the rule at the beginning of the chain
# -i up0 -o eth0 -j ACCEPT let traffic from the router into the home network
sudo iptables -I DOCKER-USER -i up0 -o eth0 -j ACCEPT
# -m conntrack --ctstate ESTABLISHED,RELATED let replies to existing connections back in
sudo iptables -I DOCKER-USER -i eth0 -o up0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo ip netns exec client curl -s -m5 -o /dev/null -w '%{http_code}\n' https://example.com/Expected:
200
These rules live in memory like the whole training ground; Appendix A sets them as part of the rebuild. On
real servers they belong in a file that is loaded after Docker – or you
configure Docker with "ip-forward-no-drop": true in
/etc/docker/daemon.json so that it doesn’t touch the
policy.
Save your progress? Sign up for free
Checkpoint
# on netz
# Docker is running, the containers have been stopped since the challenge (-a also shows stopped ones)
systemctl is-active docker
sudo docker ps -a --format '{{.Names}} {{.State}}' | sort
# client reaches the internet, the Docker rules are in place
sudo ip netns exec client curl -s -m5 -o /dev/null -w '%{http_code}\n' https://example.com/
sudo iptables -S DOCKER-USERExpected:
active
api exited
klein exited
web1 exited
200
-N DOCKER-USER
-A DOCKER-USER -i eth0 -o up0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A DOCKER-USER -i up0 -o eth0 -j ACCEPT
Clean up
You no longer need the containers and the network; Docker and the
DOCKER-USER rules stay.
# on netz
sudo docker rm -f web1 api klein
sudo docker network rm shopnet
sudo docker ps -a --format '{{.Names}}' | wc -lExpected:
web1
api
klein
shopnet
0
Interview note
“A container isn’t reachable from outside. How do you proceed?” Get
the PID with docker inspect, check with
nsenter -t <PID> -n ss -ltn what the service listens
on inside the container (often 127.0.0.1),
look at the DNAT rule of the port publishing with
nft list chain ip nat DOCKER. Bonus points: container names
resolve only in user-created networks (127.0.0.11),
-p publishes on all addresses bypassing ufw,
and Docker sets the FORWARD policy to DROP when it switches on
forwarding itself.
Enterprise note
On servers that run Docker and route something else
– a VPN gateway, libvirt VMs, a Kubernetes node with its own CNI –, the
DROP policy is a constant source of outages after the next reboot or
Docker update. Define the order in which the services start, keep your
own rules in DOCKER-USER and after every reboot check with
a test from outside, not just with “the container is running”.