Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

Docker published ports ignore ufw: why it happens and how to restrict them

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

If a container was started with -p 8080:80 and port 8080 answers from the internet while ufw status shows no rule for it (or an explicit deny), ufw is not at fault and has not failed. Docker's own documentation says that when you publish a container's ports, "traffic to and from that container gets diverted before it goes through the ufw firewall settings". The fix is to publish the port on a specific address such as 127.0.0.1, or to filter in the chain Docker provides for that purpose, DOCKER-USER.

What the symptom means

Docker's packet filtering page states that Docker and ufw "use firewall rules in ways that make them incompatible with each other". It explains the mechanism: "Docker routes container traffic in the nat table, which means that packets are diverted before it reaches the INPUT and OUTPUT chains that ufw uses." Its conclusion is: "Packets are routed before the firewall rules can be applied, effectively ignoring your firewall configuration."

So ufw allow and ufw deny rules are written for traffic addressed to the host itself. Traffic to a published container port is forwarded to the container, and a different path in the firewall handles it. The Ubuntu ufw manual page makes the same split visible: rules for traffic "not destined for the host itself but instead for traffic that should be routed/forwarded through the firewall should specify the route keyword before the rule".

Two Docker defaults matter here:

  • A published port is open to everyone unless you say otherwise. The port publishing page says "Publishing container ports is insecure by default", and that the port "becomes available not only to the Docker host, but to the outside world as well". With -p 8080:80 the mapping covers "any address on the Docker host".
  • Compose behaves the same way. The Compose file reference warns: "If you do not specify a host IP (such as 127.0.0.1), Docker binds to all interfaces (0.0.0.0), bypassing host firewall rules."

Docker's iptables page describes the DOCKER-USER chain this way: "A placeholder for user-defined rules that will be processed before rules in the DOCKER-FORWARD and DOCKER chains." Rules that you append to the FORWARD chain run after Docker's rules, so they are too late to filter published ports.

Likely causes

Cause What you see How to confirm
Port published on all addresses A service answers from other hosts although ufw status shows no allow rule docker ps PORTS column shows 0.0.0.0: before the host port
Compose ports: entry without a host IP The same, after docker compose up The short syntax "8080:80" in the Compose file
Nothing filters in DOCKER-USER Any source can reach every published port iptables -n -L DOCKER-USER --line-numbers lists no filter rules of yours
ufw rules written for the wrong path ufw deny 8080 has no effect on the container ufw show raw lists the iptables tables, so you can compare ufw's rule with Docker's rules
Direct routing enabled The container's own IP is reachable from other networks The allow-direct-routing daemon option or the com.docker.network.bridge.trusted_host_interfaces network option is configured, or direct routing to all container ports (Docker's "Gateway modes" section) is in use

Prove the cause before changing anything

All of these are read-only.

1. List the published ports.

docker ps
docker port NAME

The Docker CLI reference shows output such as 0.0.0.0:1234->9876/tcp: the part before the arrow is the host address and port. An address of 0.0.0.0 means every address. 127.0.0.1 means only the Docker host itself can use the port. Replace NAME with the container name from docker ps.

2. Read ufw's view.

ufw status verbose
ufw show raw

The ufw manual says the status command "shows basic information about the state of the firewall, as well as rules managed via the ufw command" and "does not show rules from the rules files in /etc/ufw". ufw show raw displays the filter, nat, mangle and raw tables, so you can see Docker's rules next to ufw's.

3. Read Docker's chain.

iptables -n -L DOCKER-USER --line-numbers
iptables -t nat -n -L

The iptables manual describes -n as numeric output, --line-numbers as showing each rule's position, and -t as selecting the table. If the chain lists no rules of yours, nothing filters published ports. The nat listing shows the port-mapping rules Docker created.

4. Test from outside. From a host that is not the Docker host:

curl -sI --max-time 5 http://203.0.113.10:8080/

-I fetches headers only and -m, --max-time limits the wait. A header block means the port is reachable. Use the host's real public address in place of the documentation address.

5. Check which firewall backend Docker uses. The packet filtering page says Docker "by default" creates rules with iptables, and that the daemon option firewall-backend selects iptables or nftables. Look for firewall-backend in the daemon's configuration file (the nftables page shows "firewall-backend": "nftables") or on the dockerd command line. Docker's other daemon option examples use /etc/docker/daemon.json. If it is nftables, the DOCKER-USER chain does not exist; skip the second fix below.

Fix

Fix 1: publish on 127.0.0.1 (preferred)

The port publishing page states: "If you include the localhost IP address (127.0.0.1, or ::1) with the publish flag, only the Docker host can access the published container port." The same page adds: "In releases older than 28.0.0, hosts within the same L2 segment (for example, hosts connected to the same network switch) can reach ports published to localhost." Check your engine version with docker version (read-only) before you rely on this. Put a reverse proxy on the host and let it talk to 127.0.0.1:8080.

For Compose, the ports: entry becomes "127.0.0.1:8080:80". Save the file first, make that change, and check it:

cp -p docker-compose.yml docker-compose.yml.2026-11-20
docker compose config -q

Disruptive step: the Compose up page says that when a service's configuration changed, Compose picks up the change "by stopping and recreating the containers (preserving mounted volumes)". The service is down while it recreates, and anything stored only in the container's writable layer, not in a mounted volume, is not preserved. Apply the change below in a quiet window.

docker compose config -q only validates the configuration. If it prints nothing and exits normally, apply the change:

docker compose up -d

To back out, restore the saved copy under the name you saved it, validate it, and recreate again:

cp -p docker-compose.yml.2026-11-20 docker-compose.yml
docker compose config -q
docker compose up -d

For a container started with docker run, first write down the exact run command that created it.

Disruptive step: replacing the container in the next step stops the service, and anything held only in the old container's writable layer is lost.

Then repeat that command with -p 127.0.0.1:8080:80 in place of -p 8080:80, using your normal procedure to replace the old container. Docker's pages give no command to change the mapping of a running container.

To back out, start a container again with the recorded original command.

Fix 2: allow only known sources in DOCKER-USER

Docker documents this rule for restricting access: "insert a negated rule at the top of the DOCKER-USER filter chain", which drops packets from all addresses except the ones you name. Docker's example is iptables -I DOCKER-USER -i ext_if ! -s 192.0.2.2 -j DROP, with ext_if replaced by the host's external interface.

The rule matches every forwarded packet that arrives on that interface from any other source, so it filters every published container port, not only 8080. Docker's page also shows the conntrack extension, which "may result in degraded performance"; no rule built from it is given here. If other containers must stay public (for example a web server on 443), prefer Fix 1 for the ones that should be private.

Docker also says you "may need to allow responses from servers outside the permitted external address ranges" and gives iptables -I DOCKER-USER -m state --state RELATED,ESTABLISHED -j ACCEPT, which "must be placed before DROP rules". Because -I inserts at the head of the chain, run the DROP rule first and the accept rule second, so the accept ends up above the drop.

First save the current rules and check the chain:

iptables -S DOCKER-USER > /root/docker-user.2026-11-20.txt
iptables -n -L DOCKER-USER --line-numbers

Disruptive step: Docker's pages do not say that rules inserted this way last across a reboot or a Docker restart, so do not treat them as a lasting fix. Paste the two iptables -I commands below together. The moment the DROP rule is in, every source outside 192.0.2.0/24 loses access to every published container port on eth0, including customers or monitoring you forgot to list. Keep a second session open.

Then, using the documentation range 192.0.2.0/24 for the allowed network and eth0 for the external interface:

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

To back out, delete each rule with the same rule specification you inserted:

iptables -D DOCKER-USER -i eth0 ! -s 192.0.2.0/24 -j DROP
iptables -D DOCKER-USER -m state --state RELATED,ESTABLISHED -j ACCEPT

Then run iptables -S DOCKER-USER and compare it with the saved file.

Fix 3: do not turn Docker's firewall rules off

Setting the iptables or ip6tables daemon keys to false is "not appropriate for most users" and "likely to break container networking". Docker's example of the result: containers "will not be able to access internet hosts by masquerading, but all of their ports will be accessible to hosts on the local network". It also says "It is not possible to completely prevent Docker from creating firewall rules".

Example (illustrative): web01 runs an admin interface container started with -p 8080:80. ufw allows only 22 and 443, yet a check from 198.51.100.7 with curl -sI http://203.0.113.10:8080/ returns headers. docker ps shows 0.0.0.0:8080->80/tcp, and iptables -n -L DOCKER-USER --line-numbers shows no filter rules. The administrator changes the Compose entry to "127.0.0.1:8080:80", validates it with docker compose config -q, runs docker compose up -d, and points the host's reverse proxy at 127.0.0.1:8080. The same curl from 198.51.100.7 now fails to connect.

Confirm the fix

  1. docker ps shows 127.0.0.1:8080->80/tcp for the container, not 0.0.0.0:8080.
  2. The outside test from step 4 fails to connect or times out within the five seconds you set.
  3. The access you intended still works: the reverse proxy on the host answers, and an allowed source still reaches the container.
  4. If you added DOCKER-USER rules, iptables -S DOCKER-USER shows the accept rule above the drop, and iptables -C DOCKER-USER -m state --state RELATED,ESTABLISHED -j ACCEPT succeeds. The iptables manual says -C checks whether a matching rule exists and "does not alter the existing iptables configuration".

What to watch afterwards

  • Re-read the PORTS column of docker ps after every Compose change.
  • After a reboot and after a Docker daemon restart, run iptables -S DOCKER-USER again. Docker's pages do not say whether hand-inserted rules survive either event, so test it rather than assume.
  • If you move to the nftables backend, plan the migration separately. Docker says there is "no DOCKER-USER chain" in that implementation and that rules go in separate tables, and that its nftables support is experimental.

Risks and limits

  • These commands have not been run or lab-tested.
  • The DOCKER-USER rules shown are for iptables and IPv4. Docker's daemon options iptables and ip6tables are separate, and no ip6tables command is given here.
  • Docker tells you not to modify the rules it creates. Add your own rules to DOCKER-USER, the chain Docker provides for that.
  • Docker's pages do not say whether DOCKER-USER rules inserted by hand persist across a reboot or a Docker restart.
  • Docker's page notes that packets sent to a container's own address are dropped by a rule in the raw table before DOCKER-USER sees them, and that this changes if you enable direct routing. Do not enable allow-direct-routing, trusted_host_interfaces or direct routing to all container ports (Docker's "Gateway modes") to "fix" reachability problems without understanding that effect.

Sources