Yes, Docker bypasses UFW. When you publish a port with -p 5432:5432, Docker writes its own iptables NAT rules, and the traffic is forwarded to the container instead of being delivered to the host. ufw’s rules sit in the INPUT chain, so they never see that traffic. To close the gap, bind the port to 127.0.0.1 when it doesn’t need to be public, or filter it in the DOCKER-USER chain.

This catches many people out. ufw status says only 22, 80 and 443 are allowed, yet the database is reachable from the whole internet. Here is why that happens, how to check your own server and how to fix it properly.

Why published ports skip ufw

ufw is a front end for iptables. Its rules live in chains that hang off INPUT, the chain for packets addressed to the host itself.

When Docker publishes a port (using the default iptables backend), it does three things:

  1. In the nat table it adds a DNAT rule in its DOCKER chain, which PREROUTING jumps to. This rule rewrites the destination of incoming packets from host:5432 to container-ip:5432.
  2. Because the new destination is a container on a bridge network, the kernel routes the packet through the FORWARD chain, not INPUT.
  3. In the filter table, Docker puts its own jumps at the top of FORWARD and adds an ACCEPT rule for the published port.

The packet never passes through INPUT, so ufw’s deny rules never apply. You can see Docker’s rules directly:

sudo iptables -t nat -L DOCKER -n
sudo iptables -L FORWARD -n --line-numbers
sudo iptables -L DOCKER-USER -n -v --line-numbers

Docker explains this behaviour in its packet filtering and firewalls documentation. The same thing applies to firewalld zones on the RHEL family, where published ports are also reachable unless you filter them in DOCKER-USER.

How to check which ports are really open

On the server

# What Docker publishes, per container
docker ps --format 'table {{.Names}}\t{{.Ports}}'

# What listens on the host (docker-proxy appears for published ports)
sudo ss -tlnp

An entry like 0.0.0.0:5432->5432/tcp in docker ps means the port is published on every interface, whatever ufw says.

From outside

A test from the server itself proves nothing, because local traffic follows a different path. Scan from another machine you control:

nmap -Pn -p 22,80,443,3306,5432,6379,8080,27017 203.0.113.20

Any port reported open that you didn’t mean to expose is a finding. Scan only servers you own or are authorised to test.

Fix 1: Bind to 127.0.0.1 when the port doesn’t need to be public

This is the simplest and most robust fix. If a database is only used by other containers or by apps on the same host, don’t publish it on a public interface.

docker run -d --name db -p 127.0.0.1:5432:5432 postgres:17

In Docker Compose:

services:
  db:
    image: postgres:17
    ports:
      - "127.0.0.1:5432:5432"

Even better, don’t publish the port at all. Containers on the same Docker network reach each other by service name (db:5432) without any ports: entry.

You can also change the default bind address for every container in /etc/docker/daemon.json:

{
  "ip": "127.0.0.1"
}

Restart Docker afterwards (sudo systemctl restart docker). Containers that should be public then need an explicit 0.0.0.0: in their port mapping.

To reach a database bound to localhost from your laptop, use an SSH tunnel:

ssh -L 5432:127.0.0.1:5432 user@your-server

Fix 2: Filter in the DOCKER-USER chain

Docker creates the DOCKER-USER chain and evaluates it before its own forwarding rules, and it never flushes it. This is where your rules for published ports belong.

There is a catch. By the time a packet reaches DOCKER-USER, DNAT has already rewritten its destination port to the container port. To match the port the client actually connected to, use conntrack’s original destination port.

Block port 5432 from the internet on the public interface (eth0 here; find yours with ip route get 1.1.1.1):

sudo iptables -I DOCKER-USER -i eth0 -p tcp \
  -m conntrack --ctorigdstport 5432 --ctdir ORIGINAL -j DROP

Allow a single trusted IP to reach it, inserted above the DROP rule:

sudo iptables -I DOCKER-USER -i eth0 -s 198.51.100.10 -p tcp \
  -m conntrack --ctorigdstport 5432 --ctdir ORIGINAL -j RETURN

Matching on --ctdir ORIGINAL affects only new inbound connections to that port. Replies to connections your containers open themselves are not touched.

If you prefer to block everything published and allow only a few ports, put a rule for established traffic first, or your containers lose their outbound internet access:

sudo iptables -I DOCKER-USER 1 -m conntrack --ctstate RELATED,ESTABLISHED -j RETURN
sudo iptables -I DOCKER-USER 2 -i eth0 -p tcp -m conntrack --ctorigdstport 443 --ctdir ORIGINAL -j RETURN
sudo iptables -I DOCKER-USER 3 -i eth0 -p tcp -m conntrack --ctorigdstport 80 --ctdir ORIGINAL -j RETURN
sudo iptables -I DOCKER-USER 4 -i eth0 -j DROP

Repeat the same rules with ip6tables if Docker has IPv6 enabled.

Making the rules survive a reboot

Rules added with iptables -I disappear when the server restarts. There are several options:

  • Add them to /etc/ufw/after.rules inside a *filter block that declares :DOCKER-USER - [0:0], so ufw loads them. This is the approach used by the community ufw-docker project.
  • Use a small systemd unit ordered After=docker.service that runs your iptables commands.
  • Use iptables-persistent (Debian/Ubuntu) with care. It restores a full ruleset at boot, which can conflict with the rules Docker creates itself.

Whichever you choose, reboot once and scan again from outside to confirm.

Note: recent Docker Engine releases include an optional nftables backend. When it is enabled, the iptables chains described here, including DOCKER-USER, are not used, so check the Docker documentation for your version.

A note for Coolify users

Coolify runs everything in Docker. When you enable public access on a database, Coolify starts a separate proxy container (its name ends in -proxy) that publishes the public port on the host. ufw does not filter that port. If you only needed access from your own machine, turn public access off and use an SSH tunnel. Or keep it on and restrict the port in DOCKER-USER as shown above. The Coolify documentation describes the database settings.

How Avanguard helps

  • Avanguard reports database ports published by Docker, including a Coolify public database exposed through a -proxy container, even when a firewall is active, and explains the DOCKER-USER fix.
  • It detects new open ports and databases or services listening on all interfaces.
  • The opt-in external port scan checks your server’s public IP from outside every 6 hours (about 150 common ports plus the ones that are listening). It never tries exploits or logins.

Read more about Docker and container security.

Summary

  • Check docker ps for 0.0.0.0: port mappings
  • Scan your public IP from another machine, not from the server itself
  • Bind internal services to 127.0.0.1, or don’t publish them at all
  • Filter public ports in DOCKER-USER using --ctorigdstport
  • Allow RELATED,ESTABLISHED first if you use a default-drop rule
  • Apply the same rules to IPv6 if it is enabled
  • Make the rules persistent, reboot, and scan again