Docker Port Binding and Host Firewall Bypass

Docker Networking

A container port published without a host IP ("4000:80") listens on every interface of the host, IPv4 and IPv6. Docker writes its own iptables rules to reach the container, and those rules are evaluated before the host firewall's (ufw), so the port is reachable from the internet even when ufw says it is closed.

Mechanism of Exposure

Default binding ("4000:80" -> 0.0.0.0:4000 and [::]:4000)
[Public Internet] ──► [Host eth0:4000] ──► [nat PREROUTING: DNAT] ──► [FORWARD] ──► [Container :80]
                                                                (skips INPUT; Docker's FORWARD rules accept it before ufw's)

Loopback binding ("127.0.0.1:4000:80")
[Public Internet] ──✕ (no listener on the public interface)
[Admin Workstation] ══► [SSH Tunnel :22] ──► [Host 127.0.0.1:4000] ──► [Container :80]
  1. Wildcard binding: in Compose short syntax, "HOST_PORT:CONTAINER_PORT" publishes on 0.0.0.0 and, when IPv6 is enabled, on [::]. Every interface, including the public one, accepts connections on HOST_PORT.
  2. Docker's own iptables rules: the daemon adds a DNAT rule in the nat table's PREROUTING chain (via its DOCKER chain) that rewrites the destination to the container's IP. The packet is then forwarded to the container through the FORWARD chain and never reaches INPUT, where ufw allow/deny rules live. ufw also hooks FORWARD, but Docker inserts its own jumps at the top of that chain and accepts the packet first, so ufw status can report the port closed while it is open.

Remediation

Bind to loopback

For services that don't sit behind a TLS-terminating reverse proxy meant for public traffic, publish on 127.0.0.1 only:

services:
  pgadmin4:
    ports:
      - "127.0.0.1:4000:80"

Only the host itself can then connect. Administrators reach the service through an SSH tunnel (ssh -N -L 4000:127.0.0.1:4000 user@server); see Access Loopback-Bound Docker Admin Services via SSH Tunneling.

Filter in DOCKER-USER

When a port must stay public but only for some sources, put the rules in the DOCKER-USER chain. Docker evaluates it before its own forwarding rules and never overwrites it, which is not true of rules added elsewhere.

Packets reach DOCKER-USER after DNAT, so a rule on the host port never matches. Match the container's address and port, or the original destination with conntrack:

# Allow port 4000 only from 203.0.113.10 on the public interface
iptables -I DOCKER-USER -i eth0 -p tcp -m conntrack --ctorigdstport 4000 ! -s 203.0.113.10 -j DROP

References

Review cards

Flashcard Which interfaces does a Compose port `"4000:80"` listen on?

All of them, public one included: 0.0.0.0 and, when IPv6 is enabled, [::].

Flashcard Why can `ufw` report a port closed while a Docker container is reachable on it?

Docker DNATs the packet in PREROUTING, so it is forwarded to the container instead of delivered to the host: it skips INPUT, where ufw's rules live, and Docker's own FORWARD rules accept it before ufw's.

Quiz Which Compose `ports` entry makes pgAdmin reachable only from the host itself?
  • "127.0.0.1:4000:80"
  • "4000:80"
  • "0.0.0.0:4000:80"
  • "4000:127.0.0.1:80"

The format is HOST_IP:HOST_PORT:CONTAINER_PORT. The others listen on every interface or put the IP in the wrong place.

Flashcard Why put firewall rules for published Docker ports in the `DOCKER-USER` chain?

Docker evaluates it before its own forwarding rules and never overwrites it, unlike rules added elsewhere.

Flashcard Why isn't binding to `127.0.0.1` a complete defense on its own?

It relies on Docker's own iptables rules: in Docker Engine 28.2.0–28.3.2 a firewalld reload removed them and exposed loopback-published ports (CVE-2025-54388). Keep Docker patched.

Flashcard What two threats come with exposing pgAdmin to the internet over plain HTTP?

Logins and queries cross untrusted networks in cleartext, and internet-wide scanners probe the panel for weak passwords and unpatched versions.