Skip to content
Blog

Docker and Your Firewall: iptables, nftables, and Docker Rules

Docker programs iptables and nftables itself. Learn what it writes, why editing its chains breaks networking, and how DOCKER-USER becomes your policy hook.

Published on • November 19, 2026

AI Assistant

Your host had a perfectly good firewall until docker run -p 5432:5432 ignored it. This is not a Docker bug — Docker programs the host’s packet filter itself, in chains ufw never looks at, because bridge networking physically cannot work without NAT and forwarding rules. The docs are blunt about the consequence: you should not modify the rules Docker creates, because bridge networks depend on them. The useful question for a firewall admin is then: where am I allowed to add policy? The answer is one chain (or, on nftables, your own table), and everything below is about using it correctly.

What Docker actually writes

On Linux, Docker creates firewall rules for bridge networks only — the docs note explicitly that no rules are created for ipvlan, macvlan, or host networking. With the default iptables backend, the filter table gets these custom chains:

ChainJob
DOCKER-USERPlaceholder for your rules, processed before Docker’s own chains
DOCKER-FORWARDFirst stage of Docker’s processing: pass new packets to the right chain, accept established connections
DOCKER, DOCKER-BRIDGE, DOCKER-INTERNALDecide whether a non-established packet is accepted, based on port-forwarding config
DOCKER-CTPer-bridge connection tracking
DOCKER-INGRESSSwarm routing-mesh related rules

In the FORWARD chain Docker inserts unconditional jumps to DOCKER-USER, DOCKER-FORWARD, and DOCKER-INGRESS. In the nat table it creates chain DOCKER for masquerading and port mapping — that is where -p 8080:80 becomes a DNAT rule and where outbound container traffic gets masqueraded. You can see it:

$ sudo iptables -S DOCKER-USER
-P DOCKER-USER ACCEPT
$ sudo iptables -t nat -S | grep -E 'DOCKER|MASQUERADE' | head -5
-A POSTROUTING -s 172.20.0.0/16 ! -o br-9f2c1e -j MASQUERADE
-A DOCKER ! -i br-9f2c1e -p tcp -m tcp --dport 8080 -j DNAT --to-destination 172.20.0.3:80
-A DOCKER -d 172.20.0.3/32 ! -i br-9f2c1e -o br-9f2c1e -p tcp --dport 80 -j ACCEPT

Two more behaviours that belong to Docker, not to you: it enables IP forwarding (net.ipv4.ip_forward) if not already on, and with the iptables backend it sets the FORWARD policy to DROP so the host does not become an open router. Opt out of the latter with "ip-forward-no-drop": true in daemon.json or dockerd --ip-forward-no-drop if you genuinely need routing. The experimental nftables backend does neither: it will not enable forwarding itself and will not create a default drop policy.

Why editing Docker’s chains breaks networking

Rules you append to FORWARD run after Docker’s chains, so they never see packets Docker already accepted or dropped. Deleting or reordering Docker’s DNAT/masquerade rules seems harmless until the next container starts and the rule set is wrong. And disabling Docker’s packet filter entirely is worse — the docs spell out the failure: with no replacement rules, bridge containers lose masqueraded internet access and all of their ports become reachable from the local network. Docker cannot even be fully prevented from programming rules; creating equivalent rules after the fact is described as extremely involved.

DOCKER-USER: the supported hook

DOCKER-USER is created for user-defined rules and is processed before DOCKER-FORWARD and DOCKER. This is where policy belongs — block a subnet from reaching containers, allow a monitoring host, or permit forwarding between specific interfaces.

The concrete case from the docs: only 192.0.2.0/24 may reach published container ports on interface ext_if:

$ sudo iptables -I DOCKER-USER -m state --state RELATED,ESTABLISHED -j ACCEPT
$ sudo iptables -I DOCKER-USER -i ext_if ! -s 192.0.2.0/24 -j DROP

The established-flow rule goes above the drop, or your containers lose the ability to reach the DNS and HTTP servers they need for outbound work. Ranges work too, with -m iprange required for --src-range.

One subtlety that catches everyone: by the time packets reach DOCKER-USER, DNAT has already happened, so match flags see the container’s internal IP and port, not the original destination. To match what the client actually requested, use the conntrack extension:

$ sudo iptables -I DOCKER-USER -p tcp -m conntrack --ctorigdst 198.51.100.2 --ctorigdstport 80 -j ACCEPT

The docs warn that conntrack matching may degrade performance, so use it selectively.

The limit of DOCKER-USER: packets addressed directly to a container’s own IP (“direct routed” access) are dropped by a rule in the raw table’s PREROUTING chain, which runs before filter. They never reach DOCKER-USER, so no rule there can allow them. To permit direct routing to a container’s published ports, use the network option com.docker.network.bridge.trusted_host_interfaces or the daemon option allow-direct-routing; unpublished ports stay protected. That distinction matters if a Kubernetes CNI or another host is rewriting destinations before the packet hits your firewall.

nftables: same job, different shape

Since Docker 29.0.0 you can select the backend with dockerd --firewall-backend=nftables or "firewall-backend": "nftables" in daemon.json. The docs state that for bridge networks, iptables and nftables have the same functionality — but with three sharp edges:

  • It is experimental, and it cannot be enabled while the daemon runs in Swarm mode, because overlay rules have not been migrated off iptables.
  • Docker owns tables ip docker-bridges and ip6 docker-bridges, and the docs say not to modify them directly — changes are likely to be lost.
  • There is no DOCKER-USER chain. You add your own table with base chains of the same type and hook points, and order them with base chain priority. For example, dropping everything except one source on the forward hook:
table ip my-table {
	chain my-filter-forward {
		type filter hook forward priority filter; policy accept;
		iifname "ext_if" ip saddr != 192.0.2.0/24 counter drop
	}
}

Also note that in nftables an accept is not final across base chains, so overriding Docker’s drop rules needs a firewall mark plus the daemon option --bridge-accept-fwmark. And if you are migrating from iptables, the FORWARD policy DROP Docker left behind will keep eating accepted traffic until you set it back to ACCEPT.

ufw, firewalld, and the host firewall you already have

ufw and Docker are incompatible by design. Docker routes container traffic in the nat table, which diverts packets before they reach the INPUT and OUTPUT chains ufw uses — so published ports ignore ufw rules entirely. Do not fight this with ufw rules on container ports; put policy in DOCKER-USER (or accept that published ports are governed by Docker).

firewalld is integrated instead of ignored: Docker creates a zone called docker with target ACCEPT, inserts its bridge interfaces into it, and creates a forwarding policy called docker-forwarding that allows forwarding from any zone to the docker zone. Work with that structure rather than around it.

Make your policy survive restarts

Docker rebuilds its chains and rules whenever the daemon starts, so an ad-hoc rule parked in FORWARD is the wrong home for policy. Keep it in DOCKER-USER (or your own nftables table), and make re-application part of boot: a systemd unit ordered after docker.service, or your firewall persistence tooling, so the chain contents are restored after a reboot. On Docker Desktop, none of this applies to the host at all — all VM and container traffic is funnelled through com.docker.backend, so host-level rules should target that process.

The bottom line

Docker owns the packet filter on Linux: DOCKER-USER before DOCKER-FORWARD before DOCKER, DNAT and masquerade in the nat table, and a DROP forwarding policy to keep the host from routing. Fighting that — editing Docker’s chains, disabling iptables, expecting ufw to filter container traffic — breaks networking or silently opens it up. Put your access rules in DOCKER-USER, or in your own table when using the nftables backend, remember that DNAT has already happened by the time you match, and persist the policy so the next daemon restart does not erase your afternoon.

Further reading