Docker Ports Bypass UFW? Block Public Access Safely

Updated on Nov 26, 2026
Kimberly N
9 MINS READ
Table of Contents
Why Docker Ignores UFW Rules and Fixes

Docker publishes ports by writing its own firewall rules. Those rules run before UFW sees the traffic. So a port you published with -p 8080:80 can stay open to the internet even when UFW says it is denied. This is the most common case of Docker bypassing UFW.

This guide shows you how to reproduce the problem on a test server, fix it, and check the fix from a second host. We will not turn off Docker's firewall management.

Test on an Ubuntu server with root access, such as a KVM VPS with full root access. Do not try this on production.

Why Docker Ignores UFW Rules

UFW checks traffic that goes to the server itself. Docker traffic goes to a container, so it takes a different path, and UFW never sees it. Docker says not to edit the rules Docker creates.

By default, a port published with -p 8080:80 is open on all addresses, for both IPv4 and IPv6.

This is the cause of Docker bypassing UFW. The port is open to everyone, and UFW is not in the path.

Prerequisites

Before you start, make sure to have these things ready:

  • A test Ubuntu VPS with Docker Engine and the Compose plugin installed.
  • A second host, such as a laptop or another VPS, to run tests from the outside.
  • A way to reach the VPS that does not depend on the port you test. Keep a console session open.
  • Your public interface name and your second host's IP. In this guide, they are eth0 and 203.0.113.10. Replace them with your own values.

Step 1: Check the Issue on a Test Server

First, you must check the Docker bypass UFW issue on your test server. To do this, enable UFW and block the test port with the commands below:

Bash
sudo ufw allow OpenSSHsudo ufw default deny incomingsudo ufw enablesudo ufw deny 8080/tcpsudo ufw status verbose

Then, start a test container with the following command:

Bash
docker run -d --name web-test -p 8080:80 nginx

From the second host, test the port using the VPS public IP:

Bash
curl -I --max-time 5 http://VPS_IP:8080nc -vz -w 5 VPS_IP 8080

You will see HTTP/1.1 200 OK, even though UFW blocks port 8080. This is your "before" result, so save it. 

Step 2: Use Localhost Binding to Hide the Port

If only the host or a reverse proxy on the host needs the port, you can bind it to loopback. Adding 127.0.0.1 or ::1 to the publish flag makes the port reachable only from the Docker host.

This is the simplest fix for Docker bypassing UFW, because the port is never public.

Create the Compose file below. It publishes the web port on localhost only and keeps the database on a private network:

Bash
sudo mkdir -p /opt/demosudo tee /opt/demo/compose.yaml > /dev/null <<'EOF'services:  web:    image: nginx:stable    ports:      - "127.0.0.1:8080:80"      - "[::1]:8080:80"    networks:      - frontend      - backend   db:    image: postgres:16    environment:      POSTGRES_PASSWORD: change-me    networks:      - backend networks:  frontend: {}  backend:    internal: trueEOF

Then, remove the old test container and start the new setup from the Compose file:

Bash
cd /opt/demodocker rm -f web-testdocker compose up -d
  • The db service has no ports: line, so Docker does not open it to the outside.
  • internal: true cuts the backend network off from the internet. The web and db containers can still talk to each other.

Note: On Docker older than 28.0.0, another host on the same network could reach a port published to localhost. Update Docker if yours is older.

Now test again from the second host. It should time out or be refused. On the VPS itself, curl -I http://127.0.0.1:8080 should still work.

Step 3: Control Public Docker Ports Safely

Use this step when a port must be public, but only for some IPs. First, find which firewall backend Docker uses:

Bash
cat /etc/docker/daemon.json 2>/dev/nullsudo nft list tables | grep docker-bridges

Docker can use one of two firewall tools:

  • If you see "firewall-backend": "nftables" or docker-bridges tables, Docker uses nftables. Follow the nftables method.
  • If you see none of them, Docker uses iptables. This is the default. Follow the iptables method.

Option A: iptables Rules

Docker gives you a special place for your own firewall rules. It is called DOCKER-USER, and it runs before Docker's own rules.

By the time traffic reaches this chain, Docker has already changed the port number to the container's port. So a rule for port 8080 would not match. Instead, you must use conntrack. It matches the original port that the visitor used.

This is the correct fix for Docker to bypass UFW when Docker uses iptables.

First, find your public interface with the command below:

Bash
ip route show default

Now add the rules. The -I flag puts each new rule at the top, so the last rule you add runs first. You must add the block rule first, then the allow rule. This way, your trusted IP is checked first, and everyone else is blocked:

Bash
sudo iptables -I DOCKER-USER -i eth0 -p tcp -m conntrack --ctorigdstport 8080 --ctdir ORIGINAL -j DROPsudo iptables -I DOCKER-USER -i eth0 -s 203.0.113.10 -p tcp -m conntrack --ctorigdstport 8080 --ctdir ORIGINAL -j RETURNsudo iptables -S DOCKER-USER

RETURN sends the allowed packet on to Docker's normal rules. For IPv6, add the same DROP with ip6tables:

Bash
sudo ip6tables -I DOCKER-USER -i eth0 -p tcp -m conntrack --ctorigdstport 8080 --ctdir ORIGINAL -j DROP

Conntrack can a little reduce performance on very busy servers. For most servers, you will not notice it.

These rules disappear after a reboot. To keep them, save the commands in a script. Then, create a systemd service that runs the script after Docker starts.

Create the script and make it executable with the commands below:

Bash
sudo tee /usr/local/sbin/docker-user-rules.sh > /dev/null <<'EOF'#!/bin/bash# Block port 8080 for everyone, then allow one trusted IPPORT=8080IFACE=eth0ALLOW_IP=203.0.113.10 # IPv4: add the DROP rule first, then the allow ruleiptables -C DOCKER-USER -i $IFACE -p tcp -m conntrack --ctorigdstport $PORT --ctdir ORIGINAL -j DROP 2>/dev/null || \iptables -I DOCKER-USER -i $IFACE -p tcp -m conntrack --ctorigdstport $PORT --ctdir ORIGINAL -j DROP iptables -C DOCKER-USER -i $IFACE -s $ALLOW_IP -p tcp -m conntrack --ctorigdstport $PORT --ctdir ORIGINAL -j RETURN 2>/dev/null || \iptables -I DOCKER-USER -i $IFACE -s $ALLOW_IP -p tcp -m conntrack --ctorigdstport $PORT --ctdir ORIGINAL -j RETURN # IPv6: block the portip6tables -C DOCKER-USER -i $IFACE -p tcp -m conntrack --ctorigdstport $PORT --ctdir ORIGINAL -j DROP 2>/dev/null || \ip6tables -I DOCKER-USER -i $IFACE -p tcp -m conntrack --ctorigdstport $PORT --ctdir ORIGINAL -j DROPEOF sudo chmod +x /usr/local/sbin/docker-user-rules.sh

Then, create the systemd service with the command below:

Bash
sudo tee /etc/systemd/system/docker-user-rules.service > /dev/null <<'EOF'[Unit]Description=Custom DOCKER-USER firewall rulesAfter=docker.serviceRequires=docker.service [Service]Type=oneshotExecStart=/usr/local/sbin/docker-user-rules.shRemainAfterExit=yes [Install]WantedBy=multi-user.targetEOF

Once you are done, enable and start it with:

Bash
sudo systemctl daemon-reloadsudo systemctl enable --now docker-user-rules.service

Check it with the commands below:

Bash
systemctl status docker-user-rules.servicesudo iptables -S DOCKER-USERsudo ip6tables -S DOCKER-USER

Reboot the server and run the last two commands again. Your rules should still be there. To undo it, you can run:

Bash
sudo systemctl disable --now docker-user-rules.servicesudo rm /etc/systemd/system/docker-user-rules.service /usr/local/sbin/docker-user-rules.shsudo systemctl daemon-reload

If something goes wrong, run these commands to remove the rules and go back to the old setup:

Bash
sudo iptables -D DOCKER-USER -i eth0 -s 203.0.113.10 -p tcp -m conntrack --ctorigdstport 8080 --ctdir ORIGINAL -j RETURNsudo iptables -D DOCKER-USER -i eth0 -p tcp -m conntrack --ctorigdstport 8080 --ctdir ORIGINAL -j DROPsudo ip6tables -D DOCKER-USER -i eth0 -p tcp -m conntrack --ctorigdstport 8080 --ctdir ORIGINAL -j DROP

Option B: nftables Rules

With nftables, Docker has no DOCKER-USER chain, so you make your own table for your rules. Do not edit Docker's tables.

A drop rule is final in nftables. Once a packet is dropped, no other rule can bring it back, so your block rule works even though it sits in a separate table.

This file blocks port 8080 for everyone except your trusted IP, for both IPv4 and IPv6:

Bash
sudo tee /etc/nftables-docker-filter.nft > /dev/null <<'EOF'table inet docker-public-filter {  chain forward-filter {    type filter hook forward priority filter; policy accept;    iifname "eth0" ct original proto-dst 8080 ip saddr != 203.0.113.10 drop    iifname "eth0" ct original proto-dst 8080 meta nfproto ipv6 drop  }}EOF

Load and check it with the commands below:

Bash
sudo nft -c -f /etc/nftables-docker-filter.nftsudo nft -f /etc/nftables-docker-filter.nftsudo nft list table inet docker-public-filter

To keep it after reboot, add include "/etc/nftables-docker-filter.nft" to /etc/nftables.conf and run sudo systemctl enable --now nftables.

If something goes wrong, run this command to delete your table and go back to the old setup:

Bash
sudo nft delete table inet docker-public-filter

Then, delete the include line from /etc/nftables.conf.

Whichever backend you use, never set "iptables": false in daemon.json. It will break your container networking.

Step 4: Verify Access from the Second Host

Run the tests from your trusted host and from another host, then compare the results. Test IPv6 only if your VPS has a public IPv6 address. If it does, test IPv6 too. A port can look closed on IPv4 and still be open on IPv6.

Bash
# From the allowed host (203.0.113.10): should workcurl -I --max-time 5 http://VPS_IP:8080 # From any other host: should time outcurl -I --max-time 5 http://VPS_IP:8080 # IPv6, from a host with IPv6curl -6 -I --max-time 5 "http://[VPS_IPV6]:8080"nmap -Pn -p 8080 VPS_IP

Here is what you should see before and after the fix:

Test Before After
Allowed host, IPv4 200 OK 200 OK
Other host, IPv4 200 OK Timeout
Other host, IPv6 200 OK Timeout
Container to container Works Works

Also check that SSH still works, and run docker ps to confirm your containers are healthy.

If you can't test the change, or you're not sure it's safe on a live server, read the PerLod Managed Support first. Don't assume it covers patching or fixing security incidents.

Common Docker and UFW Mistakes to Avoid

Most Docker and UFW problems come from a few repeated mistakes. These are the most common errors and fixes:

  • Matching --dport 8080 in DOCKER-USER. The packet already shows the container port, so use --ctorigdstport for the host port.
  • Using iptables -A FORWARD. Rules added there run after Docker's rules.
  • Forgetting IPv6. Check ip6tables or the inet table.
  • Keeping old DOCKER-USER rules after switching to nftables. Docker does not use that chain on the nftables backend, so the rules are ignored.

Tips: For related hardening of public services, you can also check Setting Up a Secure API Gateway on a VPS.

Conclusion

Docker's firewall rules can skip UFW, so a port you think is blocked may still be open to the internet. Keep private services on localhost, filter public ports in DOCKER-USER or nftables, and always test from another host, including IPv6. Keep a rollback command ready before you change anything on a live server.

We hope you enjoy this guide. For more details, you can check the Docker packet filtering and firewalls Docs.

Docker's rules handle the traffic before UFW sees it, so UFW deny rules do not apply.

Docker's own rules do. Your custom rules do not, unless you save them with a script, unit, or nftables config.

No. It is how Docker networking works, so you must filter with the right chain.