ufw status printed 8081/tcp DENY Anywhere, and on the next line of the terminal a client on another network namespace connected to port 8081 and got an answer: container-web. Nothing was misconfigured in the usual sense. The firewall rule was correct, the status output was accurate about the rule, and the packet simply never walked past it. That gap between what UFW reports and what the kernel actually does is the most important thing to understand about UFW on any machine that also runs Docker, and it sits on top of a few simpler mistakes that lock people out of their own servers.
What UFW is and what it sets by default
A friendly switchboard in front of iptables, with the breaker off at the factory
UFW, the Uncomplicated Firewall, writes iptables rules so nobody has to type them by hand. The Ubuntu Server firewall documentation says “The default firewall configuration tool for Ubuntu is ufw” and that “ufw by default is initially disabled.” The ufw(8) man page for Ubuntu 24.04 gives the starting state once it is switched on: “a default incoming policy of deny, a default forward policy of deny, and a default outgoing policy of allow.”
Two lines in the same man page govern almost every rule set. The first: “Rule ordering is important and the first match wins.” The second, about defaults: “existing rules will have to be migrated manually when changing the default policy.” A rule list is read top to bottom like a punch list, and the defaults only catch what falls off the end.
Everything below ran on an Ubuntu 24.04 test container with ufw 0.36.2 from the Ubuntu archive. To avoid touching the container’s own network, the tests used three Linux network namespaces joined by virtual Ethernet links: a “server” namespace running UFW, a “client” namespace on 10.50.0.0/24, and a third namespace on a bridge at 172.30.0.0/24 standing in for a container. Small Python listeners answered on port 22 (a fake SSH banner), port 8080 (a host web app) and port 80 inside the stand-in container. No real SSH daemon or Docker engine ran; where Docker’s behavior matters, the post says exactly what was reproduced and how.

Default deny, with SSH allowed first
Open the door you are standing in before locking the building
The order of commands matters more than the commands. Set the defaults, allow SSH, then enable:
$ sudo ufw default deny incoming
Default incoming policy changed to 'deny'
(be sure to update your rules accordingly)
$ sudo ufw default allow outgoing
Default outgoing policy changed to 'allow'
(be sure to update your rules accordingly)
$ sudo ufw limit 22/tcp comment 'ssh, rate limited'
Rules updated
$ sudo ufw --force enable
Firewall is active and enabled on system startup
$ sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), deny (routed)
New profiles: skip
To Action From
-- ------ ----
22/tcp LIMIT IN Anywhere # ssh, rate limited
The --force flag skips a confirmation prompt; the man page notes that “ufw will prompt when enabling the firewall while running under ssh,” and on a real remote server that prompt is worth reading rather than skipping. From the client namespace, the result:
10.50.0.1:22 SSH-2.0-OpenSSH_test
10.50.0.1:8080 no answer (timed out)
Port 22 answers. The web app on 8080, which nobody allowed, goes silent: default deny drops the packets, so the client waits until its timeout instead of getting a refusal. To open it deliberately, sudo ufw allow 8080/tcp. To see rules with numbers for deletion or insertion, sudo ufw status numbered. Before opening anything, the earlier post on finding what is using a port is the way to learn what is listening in the first place.
The lockout that waits until logout
The current session survives, which is exactly what makes it dangerous
The classic UFW disaster is enabling a default-deny firewall over SSH without an SSH rule. What makes it nasty is the timing. A test held a TCP session open from the client to an echo service on port 2222, then deleted the rule allowing 2222 while the session was still connected:
session before rule removal -> b'echo:before'
Rule deleted
same session after rule removal -> b'echo:after'
new session -> TimeoutError timed out
The connection that already existed kept working; only new connections hit the missing rule. So the administrator who just cut off SSH sees nothing wrong, finishes up, logs out, and finds the door locked from the outside. Any test of a new firewall should open a second, fresh SSH session before the first one is closed. On a cloud server, the provider’s web console is the spare key; on a home box, it is a monitor and keyboard.

Rate limiting with ufw limit
Six knocks in thirty seconds and the door stops answering
The man page describes limit this way: “ufw will normally allow the connection but will deny connections if an IP address attempts to initiate 6 or more connections within 30 seconds.” After waiting out the window, the client opened eight connections to port 22 in a row:
attempt 1: SSH-2.0-OpenSSH_test
attempt 2: SSH-2.0-OpenSSH_test
attempt 3: SSH-2.0-OpenSSH_test
attempt 4: SSH-2.0-OpenSSH_test
attempt 5: SSH-2.0-OpenSSH_test
attempt 6: ConnectionRefusedError: [Errno 111] Connection refused
attempt 7: ConnectionRefusedError: [Errno 111] Connection refused
attempt 8: ConnectionRefusedError: [Errno 111] Connection refused
Five got through; the sixth was refused, as documented. The rules UFW generated show the mechanism, the kernel’s recent module counting new connections per source address:
-A ufw-user-input -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -m recent --set --name DEFAULT --mask 255.255.255.255 --rsource
-A ufw-user-input -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -m recent --update --seconds 30 --hitcount 6 --name DEFAULT --mask 255.255.255.255 --rsource -j ufw-user-limit
-A ufw-user-limit -m limit --limit 3/min -j LOG --log-prefix "[UFW LIMIT BLOCK] "
-A ufw-user-limit -j REJECT --reject-with icmp-port-unreachable
The generated rules settle two practical questions. Blocked attempts are rejected, not dropped, so a client sees an instant refusal rather than a hang. And every new attempt, including a blocked one, updates the counter, so a source that keeps retrying keeps itself blocked until it goes quiet for thirty seconds. In the first run of this test, an earlier probe from the same address had already used one slot, and the cutoff arrived at attempt five instead of six. The count is per source address, not per session, and it does not care who is asking.
Docker publishes ports around UFW
The firewall guards the front door while the freight entrance has its own key
Docker’s own packet filtering documentation is direct about it: “Docker and ufw use firewall rules in ways that make them incompatible with each other,” and with published ports, “traffic to and from that container gets diverted before it goes through the ufw firewall settings.” The port publishing page adds that “Publishing container ports is insecure by default” and that, without an explicit address, “the Docker daemon publishes ports to all host addresses.”
The reason is the packet path. UFW’s allow and deny rules for the host live on the INPUT chain, for traffic addressed to the host itself. A published Docker port rewrites the destination in the nat table to the container’s address; the Docker docs on iptables chains say Docker “creates chain DOCKER and adds rules to implement masquerading and port-mapping.” After that rewrite the packet is no longer for the host. It is forwarded traffic on the FORWARD chain, so UFW’s host rules on INPUT never get a look at it.
No Docker engine ran in the test. Instead, the server namespace got the same two pieces by hand: a DNAT rule sending 10.50.0.1:8081 to the stand-in container at 172.30.0.2:80, and accept rules inserted at the top of FORWARD, a hand-built stand-in for the forwarding path the Docker docs describe for a published port. A UFW deny rule for 8081 went in first.
$ sudo ufw deny 8081/tcp comment 'should be closed'
== DNAT only, no Docker-style FORWARD accept:
10.50.0.1:8081 no answer (timed out)
== with Docker-style accept rules inserted at top of FORWARD:
10.50.0.1:8081 container-web
And UFW’s view at the same moment:
22/tcp LIMIT Anywhere # ssh, rate limited
8081/tcp DENY Anywhere # should be closed
Chain FORWARD (policy DROP)
num target prot opt source destination
1 ACCEPT 0 -- 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
2 DOCKER-SIM 0 -- 0.0.0.0/0 0.0.0.0/0
3 ufw-before-logging-forward 0 -- 0.0.0.0/0 0.0.0.0/0
4 ufw-before-forward 0 -- 0.0.0.0/0 0.0.0.0/0
With only the DNAT rule, UFW’s routed default of deny caught the traffic. The DENY rule for 8081 had nothing to do with it; it sits on INPUT and never saw the packet. Once accept rules sat above UFW’s chains, the stand-in container answered while UFW kept printing DENY. First match wins, and the first match was not UFW’s.

Where people get burned
Real failure modes, most of them reproduced above
Enabling without an SSH rule. Reproduced: the open session survives, the next one does not.
Trusting ufw status on a Docker host. Reproduced with the stand-in rules: the status listing showed DENY while the port answered. The listing is a record of UFW’s rules, not a port scan. A check from a second machine, with nc -vz host port or nmap, is what shows which ports are reachable.
Publishing to every address by default. A Compose file with ports: "8080:80" binds to all host addresses. The Docker docs show the fix, docker run -p 127.0.0.1:8080:80 -p '[::1]:8080:80' nginx, so that “only the Docker host can access the published container port.” The same page warns that before Docker 28.0.0, hosts on the same local network segment could still reach ports published to localhost. A reverse proxy in front, as in the Traefik, Caddy and Nginx Proxy Manager comparison, lets the app ports stay on loopback and only 80 and 443 face the network.
Filtering in the wrong chain. Docker’s iptables docs point to DOCKER-USER, “A placeholder for user-defined rules that will be processed before rules in the DOCKER-FORWARD and DOCKER chains.” Rules there see packets after DNAT, so matching the published port needs the conntrack extension; the docs show -m conntrack --ctorigdst 198.51.100.2 --ctorigdstport 80. In the test, a chain placed first in FORWARD in the same position, with -i vsrv -p tcp -m conntrack --ctorigdstport 8081 --ctdir ORIGINAL ! -s 10.50.0.99 -j DROP, made port 8081 go silent again for the client. The docs also warn that conntrack matching “may result in degraded performance.”
Rate limiting the automation. A deployment tool or backup job that opens a fresh SSH connection per task can trip six-in-thirty and fail halfway with “connection refused,” as the limit test shows.
Editing Docker’s rules by hand. The Docker docs say “you should not modify the rules created by Docker.” They are rebuilt when the daemon restarts, and hand edits vanish with them.
The case against UFW on a container host
A simple tool for a box where nothing about the plumbing is simple
The strongest argument against UFW here is that it gives a clean, readable answer to a question the kernel is no longer asking. On a machine running only SSH and a couple of daemons, UFW’s model of allow and deny on incoming ports matches reality, and it is hard to beat. On a Docker host, the packets that matter take a route UFW does not manage, and the readable output becomes a padlock on a gate in a fence with a hole in it.
The alternatives each move the burden somewhere. Binding published ports to 127.0.0.1 is reliable, but it lives in every Compose file and gets forgotten in the next one copied from a README. Rules in DOCKER-USER work, but they are raw iptables with conntrack matches, which is the complexity UFW was installed to avoid. A firewall upstream, on the router or the cloud provider’s security group, does not care what chains the host builds, and is arguably the most honest layer for a home lab, at the cost of a second place to keep rules in sync.
UFW still earns its spot as the first layer: default deny, SSH limited, and nothing else open until it is needed. The earlier guides to self-hosting with Docker Compose and automatic security updates cover the parts that sit beside it.
The test namespace still shows 8081/tcp DENY Anywhere. Which ports on the nearest Docker host answer from another machine, and how many of them appear anywhere in ufw status?
Sources
- Ubuntu Server documentation, Firewalls: https://documentation.ubuntu.com/server/how-to/security/firewalls/
- ufw(8) man page, Ubuntu 24.04 (noble): https://manpages.ubuntu.com/manpages/noble/man8/ufw.8.html
- Docker documentation, Packet filtering and firewalls: https://docs.docker.com/engine/network/packet-filtering-firewalls/
- Docker documentation, Port publishing and mapping: https://docs.docker.com/engine/network/port-publishing/
- Docker documentation, Docker with iptables: https://docs.docker.com/engine/network/firewall-iptables/
