Docker and your firewall: why published ports skip your INPUT rules

By TurnUpHosting Web Services LLC · · #docker #firewall #linux

A common surprise on Docker hosts: you block an address in the firewall, or deny a port with ufw, and the container is still reachable. Nothing is broken; the traffic simply never passes through the rules you wrote.

Why it happens

When you publish a port (-p 8080:80), Docker adds a DNAT rule in the nat table's PREROUTING chain. Incoming packets for that port are rewritten to the container's internal address before the routing decision. Because the destination is now a container, not the host, the packet is forwarded, not delivered locally. It goes through the FORWARD chain and never touches INPUT, which is where most host firewalls, including ufw's defaults, put their rules.

The right place: DOCKER-USER

Docker creates a chain called DOCKER-USER and evaluates it before its own forwarding rules. It's designed for your rules and Docker never flushes it. To block an address from every published container:

iptables -I DOCKER-USER -s 203.0.113.7 -j DROP

For large block lists, match an ipset instead of one rule per address:

iptables -I DOCKER-USER -m set --match-set blocklist src -j DROP

Remember IPv6 (ip6tables) if Docker has IPv6 enabled.

Other good habits

  • Bind internal services to localhost: -p 127.0.0.1:5432:5432 keeps a database off the internet entirely.
  • Put a reverse proxy in front: publish only 80 and 443 from one proxy container and keep the rest on internal Docker networks.
  • Check what's exposed: docker ps --format '{{.Names}} {{.Ports}}' and ss -tlnp.
  • Filter as early as possible: XDP drops happen in the network driver, before NAT, so they apply to containers and the host alike.

TurnUpSecurity Shield detects Docker and adds its block rule to DOCKER-USER as well as INPUT, so blocked attackers can't reach published containers either, and its XDP filter covers both.