Dev.to Security 🔐 Cybersecurity 👁 0 📖 5 min read

The Docker port stays open with ufw

A port published with docker run -p stays reachable from outside even with ufw active and a deny rule on that port. The test below ran on 18 November 2026 on Ubuntu 24.04.5 LTS, with Docker Engine 29.10.0 and ufw 0.36.2.

A port published with docker run -p stays reachable from outside even with ufw active and a deny rule on that port. The test below ran on 18 November 2026 on Ubuntu 24.04.5 LTS, with Docker Engine 29.10.0 and ufw 0.36.2. The request came from another network namespace, with source 203.0.113.10, which is not in the private ranges.

The Docker docs describe why: Docker diverts the packet in the nat table before the INPUT and OUTPUT chains, which are the ones ufw uses. The Coolify firewall docs repeat the warning and point at ufw-docker when the VPS has no firewall at the provider.

What the test published

The container is the official Apache, tag httpd:2.4-alpine, digest sha256:32bd0639bcc4917ef1fd9363a1d9dd2dd9c498565118b062a10d51e43a5c3a24. Port 80 in the container is on port 8088 of the server:

sudo docker pull httpd:2.4-alpine
sudo docker run -d --name porta-teste -p 8088:80 httpd:2.4-alpine

ufw was left like this, with 8088 explicitly denied. The forwarding policy was deny as well:

sudo ufw allow 22/tcp
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw --force enable
sudo ufw deny 8088/tcp
sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), deny (routed)

     To                         Action      From
     --                         ------      ----
[ 1] 22/tcp                     ALLOW IN    Anywhere
[ 2] 8088/tcp                   DENY IN     Anywhere
[ 3] 22/tcp (v6)                ALLOW IN    Anywhere (v6)
[ 4] 8088/tcp (v6)              DENY IN     Anywhere (v6)

The request that should not have passed

You cannot see this hole with curl to 127.0.0.1 on the server itself. The request has to arrive from outside. In the lab, a wan namespace played that part. The address 203.0.113.0/24 is from the documentation range, and the ping between the two sides answered.

sudo ip netns add wan
sudo ip link add veth-wan type veth peer name veth-host
sudo ip link set veth-wan netns wan
sudo ip addr add 203.0.113.1/24 dev veth-host
sudo ip link set veth-host up
sudo ip netns exec wan ip link set lo up
sudo ip netns exec wan ip addr add 203.0.113.10/24 dev veth-wan
sudo ip netns exec wan ip link set veth-wan up
sudo ip netns exec wan ip route add default via 203.0.113.1

The request, leaving from 203.0.113.10:

sudo ip netns exec wan curl -sS -D - -o /dev/null --max-time 8 http://203.0.113.1:8088/

The response, with ufw active and the DENY rule on 8088:

HTTP/1.1 200 OK
Server: Apache/2.4.71 (Unix)

On your VPS, the equivalent is to run the same curl from another computer on the internet, replacing 203.0.113.1 with the public IPv4.

The fix

ufw-docker writes rules in the DOCKER-USER chain, which Docker consults before it accepts the forward. The tested copy is commit 020a8699f95592561f254d8d4ad1bb40d401dfc7. The script's SHA-256 was 643e56b080567c567b4aa28650196849b2a2da5dd0473fd3e5216b0886035ab0. The project README points at master; the command below pins the file this test ran.

sudo curl -fsSL -o /usr/local/bin/ufw-docker \
  https://raw.githubusercontent.com/chaifeng/ufw-docker/020a8699f95592561f254d8d4ad1bb40d401dfc7/ufw-docker
sudo chmod 755 /usr/local/bin/ufw-docker
sha256sum /usr/local/bin/ufw-docker
sudo ufw-docker install
sudo ufw reload

install showed the diff of /etc/ufw/after.rules and of /etc/ufw/after6.rules, kept a copy of each file and asked for sudo systemctl restart ufw. sudo ufw reload was enough for the block to apply. The same curl from outside then failed:

curl: (28) Connection timed out after 8002 milliseconds

From the server itself, curl http://127.0.0.1:8088/ kept answering HTTP/1.1 200 OK. The service did not go down. What closed was new access coming from outside.

The rules written by install return early for traffic sourced from 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. A curl made from an address in those ranges, or from the server itself, does not show whether the internet was blocked. The timeout in this test came from 203.0.113.10, outside them.

To open on purpose, the rule uses the container port. Here Apache listens on 80, and the host shows 8088. The rule is on 80:

sudo ufw route allow proto tcp from any to any port 80

ufw status numbered then had 80/tcp ALLOW FWD and kept 8088/tcp DENY IN. Even so, the outside curl to port 8088 again received HTTP/1.1 200 OK with Server: Apache/2.4.71 (Unix). The DENY IN on the host port is not what decides.

ufw allow 8088/tcp is not the container's rule. That rule goes into the host's input chain. The packet Docker published does not pass through it. What opens the port is ufw route allow on the port the process listens on inside the container.

On Coolify

The same hole shows up on the panel. With ufw active and no rule for port 8000, http://203.0.113.1:8000/ still answered HTTP/1.1 302 Found to /login. After ufw-docker and ufw reload, that request started timing out, the same as Apache.

docker ps shows 0.0.0.0:8000->8080/tcp on the Coolify container. The rule that reopened the panel was on 8080, not on 8000:

sudo ufw route allow proto tcp from any to any port 8080

The response went back to HTTP/1.1 302 Found to /login.

Ports 80 and 443 of the proxy and ports 6001 and 6002 follow the same rule: the port in route allow is the container's. The list this test opened, and what each one answered, is in How to install Coolify on an Ubuntu VPS.

If the VPS has a firewall in the provider's panel, use that firewall and keep ufw as a second layer. It drops the packet before Docker. The n8n install guide avoids the problem another way: only Caddy publishes a port, and the rest stays on Compose's internal network. The steps are How to install n8n with Docker on a VPS.

What to check

  • From another computer, the published port stops connecting after ufw reload. A timeout is a block. 200 is an open port.
  • On the server itself, curl http://127.0.0.1:PORT/ can still answer 200. That does not prove the internet can reach the port, and it does not prove it stopped reaching it.
  • ufw status shows ALLOW FWD on the container port when the opening is the ufw-docker one. A DENY IN on the host port can stay in the list and not change the result.
  • Keep SSH open in a session that is already connected until you confirm access from another session. The ufw allow 22/tcp rule in this test stayed in place, and the session continued.

The plan beside this article covers a VPS where this Docker runs. If you are still choosing the machine, start at Docker VPS.

Originally published at https://nuvits.net/en/blog/docker-port-still-open-with-ufw

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.