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.
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:
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:
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
iprouteshowdefault
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:
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
sudotee/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-CDOCKER-USER-i$IFACE-ptcp-mconntrack--ctorigdstport$PORT--ctdirORIGINAL-jDROP2>/dev/null||\iptables-IDOCKER-USER-i$IFACE-ptcp-mconntrack--ctorigdstport$PORT--ctdirORIGINAL-jDROPiptables-CDOCKER-USER-i$IFACE-s$ALLOW_IP-ptcp-mconntrack--ctorigdstport$PORT--ctdirORIGINAL-jRETURN2>/dev/null||\iptables-IDOCKER-USER-i$IFACE-s$ALLOW_IP-ptcp-mconntrack--ctorigdstport$PORT--ctdirORIGINAL-jRETURN# IPv6: block the portip6tables-CDOCKER-USER-i$IFACE-ptcp-mconntrack--ctorigdstport$PORT--ctdirORIGINAL-jDROP2>/dev/null||\ip6tables-IDOCKER-USER-i$IFACE-ptcp-mconntrack--ctorigdstport$PORT--ctdirORIGINAL-jDROPEOFsudochmod+x/usr/local/sbin/docker-user-rules.sh
Then, create the systemd service with the command below:
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:
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
sudonftdeletetableinetdocker-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-time5http://VPS_IP:8080# From any other host: should time outcurl-I--max-time5http://VPS_IP:8080# IPv6, from a host with IPv6curl-6-I--max-time5"http://[VPS_IPV6]:8080"nmap-Pn-p8080VPS_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.
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 use cookies to run PerLod and to understand how the site is used. You can accept everything, or open Manage to turn individual categories off. Cookie policy.