Q: Your security team configured Ubuntu UFW to block external access to port 5432, intending PostgreSQL to be accessible only from internal private IPs. An engineer launches a PostgreSQL container with `docker run -p 5432:5432 postgres`. An external vulnerability scan discovers that port 5432 is publicly exposed to the entire internet! SREs attempted setting `"iptables": false` in `/etc/docker/daemon.json`, which broke all container outbound internet access and DNS resolution. You must fix the firewall bypass properly using Docker's native `DOCKER-USER` iptables chain.
Diagnose and resolve the critical vulnerability where Docker bypasses host UFW/firewalld rules by inserting raw iptables PREROUTING rules. Configure the `DOCKER-USER` chain properly.
Want to master this scenario in a live sandbox? KodeKloud's Docker Certified Associate (DCA) Hands-On Lab Course covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Analyze Why Docker Bypasses Host Firewalls (UFW / Firewalld)
UFW and firewalld manage the `INPUT` chain. However, Docker published ports are routed through the iptables `PREROUTING` NAT table directly into the `FORWARD` chain, completely bypassing the `INPUT` chain where UFW rules reside.
<!-- Linux Kernel Packet Filtering Flow -->
Incoming Packet on Port 5432
│
├──> PREROUTING (Docker DNAT translates host IP:5432 to container IP:5432)
│
├──> FORWARD Chain <=== DOCKER RULES LIVE HERE (UFW default allows forwarded packets!)
│ │
│ └───> Container Receives Packet! (UFW INPUT chain was completely bypassed!)
│
└──> INPUT Chain <=== UFW Rules Live Here (Never evaluated for forwarded traffic!)
Understand the Disastrous Side Effects of iptables: false
Setting `"iptables": false` in `daemon.json` prevents Docker from modifying iptables. However, this also disables Docker's outbound NAT (MASQUERADE) and bridge routing rules, causing containers to lose internet connectivity, DNS resolution, and container-to-container communication unless hundreds of manual iptables rules are maintained.
# daemon.json anti-pattern:
# { "iptables": false } <-- Breaks outbound NAT and container networking!
Implement Proper Access Control in the DOCKER-USER Chain
Docker reserves the `DOCKER-USER` iptables chain specifically for user-defined firewall rules. The `DOCKER-USER` chain is evaluated before any Docker-generated forwarding rules. Insert rules here to restrict access to trusted subnets.
# Flush existing custom rules
sudo iptables -F DOCKER-USER
# Allow established and related connections
sudo iptables -A DOCKER-USER -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
# Allow port 5432 only from trusted internal subnet (10.0.0.0/16)
sudo iptables -A DOCKER-USER -p tcp -s 10.0.0.0/16 --dport 5432 -j ACCEPT
# Drop all other external traffic to port 5432
sudo iptables -A DOCKER-USER -p tcp --dport 5432 -j DROP
# Return other traffic to Docker default chains
sudo iptables -A DOCKER-USER -j RETURN
Bind Containers Directly to Internal Interfaces
For maximum safety at launch time, explicitly bind published ports to `127.0.0.1` or the specific private network interface IP rather than the default wildcard `0.0.0.0`.
# Bind to localhost only
docker run -d -p 127.0.0.1:5432:5432 postgres:16
# Or bind to private VPC interface
docker run -d -p 10.0.1.50:5432:5432 postgres:16
- W
- e
- r
- e
- s
- o
- l
- v
- e
- d
- a
- c
- r
- i
- t
- i
- c
- a
- l
- s
- e
- c
- u
- r
- i
- t
- y
- e
- x
- p
- o
- s
- u
- r
- e
- w
- h
- e
- r
- e
- D
- o
- c
- k
- e
- r
- w
- a
- s
- b
- y
- p
- a
- s
- s
- i
- n
- g
- U
- F
- W
- r
- u
- l
- e
- s
- t
- o
- e
- x
- p
- o
- s
- e
- i
- n
- t
- e
- r
- n
- a
- l
- d
- a
- t
- a
- b
- a
- s
- e
- s
- t
- o
- t
- h
- e
- p
- u
- b
- l
- i
- c
- i
- n
- t
- e
- r
- n
- e
- t
- .
- I
- n
- s
- t
- e
- a
- d
- o
- f
- b
- r
- e
- a
- k
- i
- n
- g
- c
- o
- n
- t
- a
- i
- n
- e
- r
- n
- e
- t
- w
- o
- r
- k
- i
- n
- g
- w
- i
- t
- h
- `
- i
- p
- t
- a
- b
- l
- e
- s
- :
- f
- a
- l
- s
- e
- `
- ,
- w
- e
- i
- m
- p
- l
- e
- m
- e
- n
- t
- e
- d
- g
- r
- a
- n
- u
- l
- a
- r
- C
- I
- D
- R
- f
- i
- l
- t
- e
- r
- i
- n
- g
- i
- n
- D
- o
- c
- k
- e
- r
- '
- s
- d
- e
- s
- i
- g
- n
- a
- t
- e
- d
- `
- D
- O
- C
- K
- E
- R
- -
- U
- S
- E
- R
- `
- i
- p
- t
- a
- b
- l
- e
- s
- c
- h
- a
- i
- n
- a
- n
- d
- m
- a
- n
- d
- a
- t
- e
- d
- b
- i
- n
- d
- i
- n
- g
- p
- u
- b
- l
- i
- s
- h
- e
- d
- p
- o
- r
- t
- s
- t
- o
- i
- n
- t
- e
- r
- n
- a
- l
- V
- P
- C
- i
- n
- t
- e
- r
- f
- a
- c
- e
- I
- P
- s
- .