What you’ll get
Build a container network by hand from kernel primitives (ip netns, veth, and a br-app bridge), add nftables drop rules on the host for inbound port 8080 and outbound 9090, and see what tcpdump captures:
# The veth pair links the br-app bridge on the host (index 42) to eth0 inside the st_app namespace (index 41):
ip -o link show veth-app
# 42: veth-app@if41: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br-app state UP ... link-netns st_app
ip netns exec st_app ip -o link show eth0
# 41: eth0@if42: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP ... link-netns st_host
# Inbound SYN to 8080 is dropped in input, outbound to 9090 in output:
sudo tcpdump -nn -i br-app 'tcp port 8080 or tcp port 9090'
# 20:05:31.293244 IP 10.0.50.10.37232 > 10.0.50.1.8080: Flags [S], seq 2136655328, win 64240, length 0
# 20:05:32.322323 IP 10.0.50.10.37232 > 10.0.50.1.8080: Flags [S], seq 2136655328, win 64240, length 0
# (no outbound traffic to port 9090 appears in the dump, even though the output drop counter grew by 2 packets)
A single line of ip link show lays out the entire virtual cable between the host and a container:
netns, a veth pair, and a br-* bridge on the host.How container networking is built: netns, veth, and bridge
Docker does not invent its own network subsystem — it combines three Linux kernel mechanisms (Docker networks):
netns(Network Namespace) — an isolated copy of the entire network stack: its own interfaces, its own127.0.0.1, its own routing table, its own ARP/NDP cache, and its ownnftablesrules. Each container gets its ownnetns.veth(Virtual Ethernet Pair) — a virtual patch cord made of two interfaces. A packet sent into one end comes straight out of the other. One end moves into the container’snetnsaseth0; the other stays on the host (veth…).bridge(docker0,br-*) — a software L2 switch in the kernel (MAC, switch, ARP, and VLAN). The host ends ofvethpairs for all containers on a network plug into it; the bridge itself holds the gateway IP (such as10.0.50.1/24) through which containers reach the outside via host NAT (NAT, DNAT, and conntrack).
Packet path through the kernel: netfilter hooks and AF_PACKET
Every packet entering or leaving Linux follows a fixed path across five netfilter hooks (where nftables,
iptables, and ufw operate):
Incoming frame (NIC / veth)
│
├──► [AF_PACKET: tcpdump sees ALL inbound traffic here — BEFORE the firewall]
▼
[prerouting] (DNAT: e.g. Docker -p 8080:80 rewrites Dst IP to the container IP)
│
▼
Routing decision (ip route): local host or forwarded?
├── for this host ──► [input] ──────► local socket (nginx, sshd)
│ │
│ ▼ (app reply)
│ [output] (outbound filtering)
│ │
└── forwarded ──────► [forward] ◄────────────┘ (shared exit path)
│
▼
[postrouting] (SNAT / masquerade: rewrites Src IP)
│
├──► [AF_PACKET: tcpdump sees outbound AFTER the firewall]
▼
Outgoing frame (NIC / veth)
Two facts fall straight out of this diagram and save hours during network debugging:
tcpdumpasymmetry. Inbound traffic hitsAF_PACKET(tcpdump) beforepreroutingandinput, while outbound traffic hits it afteroutputandpostrouting. A packet dropped ininputstill shows up in the capture; a packet dropped inoutputnever does.- The
inputvsforwardsplit. Oncepreroutingapplies DNAT toward a container (or the host acts as a VPN router), the packet entersforwardand never touchesinput. Filter published container ports inforward(nftables).
- Container✓ SYN to :8080
- AF_PACKET✓ tcpdump saw SYN
- netfilter input✕ counter 2 · drop
- Application— nothing arrived
# Both SYNs appear in tcpdump on br-app: 20:05:31.293244 IP 10.0.50.10.37232 > 10.0.50.1.8080: Flags [S], length 0 20:05:32.322323 IP 10.0.50.10.37232 > 10.0.50.1.8080: Flags [S], length 0 # Meanwhile the nftables input counter climbed: tcp dport 8080 counter packets 2 bytes 120 drop
The packet was copied to tcpdump first, then dropped by the input chain right after. Seeing an inbound SYN in tcpdump does not prove the firewall allowed it.
sudo nft list ruleset (watch counter values on drop rules)
- curl on host✓ SYN to :9090
- netfilter output✕ counter 2 · drop
- AF_PACKET— tcpdump is empty
# Not a single packet to port 9090 in tcpdump on br-app! # Yet the nftables output counter shows where they went: tcp dport 9090 counter packets 2 bytes 120 drop
On the outbound path, the firewall runs ahead of AF_PACKET. When an app sends traffic and tcpdump stays blank, check the output chain and the routing table.
sudo nft list ruleset and ip route get 10.0.50.10
- eth0 + AF_PACKET✓ SYN to :8080
- prerouting DNAT✓ -> 10.0.50.10:80
- input (ufw deny)— bypassed
- forward -> veth✓ reached container
# Routing after DNAT sends the packet into forward, skipping input: # eth0 -> prerouting (DNAT) -> forward -> postrouting -> br-app -> veth-app -> container
Port 8080 blocked only in input stays open for traffic headed to a container. Filter that traffic in forward (or publish on 127.0.0.1).
sudo nft list ruleset (forward and DOCKER-USER chains)
If it doesn’t work
tcpdump -nn -i eth0 shows incoming SYN packets from a client, but the app stays silent and the client times out
- Cause
tcpdumpcaptures incoming frames via anAF_PACKETsocket at the interface entrance — beforenetfilterchains run. The packet reached the dump, then adroprule innftablesorufwdiscarded it microseconds later.- Fix
- Inspect rules and packet counters with
sudo nft list ruleset: look for a climbingdropcounter in theinputandforwardhooks.
A program on the server sends packets outbound and times out, while tcpdump -nn -i eth0 shows nothing at all
- Cause
- On the outbound path,
netfilterhooks (outputandpostrouting) run before the frame reaches the driver andAF_PACKET. A packet dropped inoutputor missing a route never reachestcpdump. - Fix
- Check the
outputchain insudo nft list rulesetand verify the route withip route get <IP>.
Port 8080 is blocked in ufw (input), yet a Docker container with -p 8080:80 stays reachable from outside
- Cause
- In the
preroutinghook, Docker rewrites the destination address to the container IP (DNAT). After routing, the packet enters theforwardhook towardbr-*and bypassesinputcompletely. - Fix
- Publish the port on
127.0.0.1:8080:80or add a filtering rule toforward/DOCKER-USER(Docker ports bypass the input chain, nftables).
You need tcpdump or ss inside a scratch/distroless container that has no bash or network tools
- Cause
- A container’s network stack is a regular Linux kernel
netnstied to its process PID, so host tools can enter it directly. - Fix
- Get the PID (
docker inspect -f '{{.State.Pid}}' <container>) and runsudo nsenter -t <PID> -n ss -tulnporsudo nsenter -t <PID> -n tcpdump -nn -i eth0from the host.
The host shows dozens of veth…@if… interfaces and you cannot tell which belongs to which container
- Cause
- Docker creates a
vethpair: one end lives inside the container’snetns(eth0@if42), the other sits on the host (veth…@if41) attached to abr-*bridge. - Fix
- Read the number after
@iffromip linkinside the container (nsenter -t <PID> -n ip -o link show eth0) and match that index inip -o linkon the host.