Docker bypassing UFW firewall Ubuntu fix: trace ports safely
Docker bypassing UFW firewall Ubuntu fix: trace published ports, verify the forwarding path, and apply a narrow rule without breaking SSH or containers.
Docker is usually not ignoring UFW; a published port can be destination-translated and sent through the kernel’s FORWARD path instead of the ordinary UFW INPUT path. Preserve SSH and capture the current state first, then use the narrowest durable fix: bind the port to 127.0.0.1 or a private address, restrict its source traffic, or add a controlled Docker-aware forwarding policy.
Why the container is reachable despite your UFW rule
“Docker bypassing UFW” usually means the port is published through Docker’s bridge and forwarding rules, not that every UFW rule has failed. A declaration such as -p 8080:80 or ports: - "8080:80" commonly publishes the host port on all host addresses unless you specify a bind address. Docker then creates netfilter rules for destination translation and forwarding. This is Docker’s normal design, not proof that the application itself is listening on the public interface.
A normal host service may be checked through the host’s INPUT path. An externally published container port can instead arrive on a network interface, be translated to the container’s bridge address, and traverse FORWARD. That is why ufw deny 8080 may not stop a Docker-published port when the Docker rules are processed outside the UFW input policy.
Reproduce the exposure from a genuinely separate network, such as another server or a mobile connection:
nc -vz SERVER_IP PORT
curl --connect-timeout 5 http://SERVER_IP:PORTUse the command appropriate for the service. Testing from the host can mislead you because loopback traffic and local routing do not represent an Internet client. A port published on 0.0.0.0 is exposed by declaration, before UFW’s intended access policy is considered.
Keep the layers separate: the application may be listening inside the container; Docker may publish it; Ubuntu may forward it; or AWS, Azure, Hetzner, a router, load balancer, or another upstream firewall may allow or block traffic before Ubuntu sees it.
Before changing anything, protect SSH and capture the current state
Keep the current SSH session open and establish a second session before changing firewall or networking policy. Do not casually restart networking over remote SSH. Have a provider console, serial console, or another tested recovery route available if possible.
Record the SSH port and current client address, and preserve the existing SSH allow rule. Then capture the relevant host, UFW, Docker, socket, interface, and route state:
sudo ufw status verbose
sudo ufw show raw
sudo docker version
sudo docker ps --format 'table {{.Names}}\t{{.Ports}}\t{{.Networks}}'
sudo docker network ls
sudo ss -lntup
ip -br address
ip route
sudo docker inspect CONTAINER_NAMEReplace CONTAINER_NAME with the actual container name. In docker ps, look for values such as 0.0.0.0:8080->80/tcp, [::]:8080->80/tcp, or 127.0.0.1:8080->80/tcp. The first forms indicate wildcard host exposure; the last restricts the published host side to loopback.
Save the Compose file or docker run command that defines the port mapping. That declaration must be the source of truth, because recreating a container can undo a manual workaround. Before editing UFW, Docker, iptables, or nftables, record the current rules using the backend your installation uses. Ubuntu and Docker versions can differ in whether commands display iptables compatibility rules backed by nftables, so verify the active backend and the current official documentation for your installed versions in 2026.
Do not flush iptables, nftables, UFW, or Docker chains as a first fix. A flush can remove SSH access and Docker’s required networking rules without giving you a reliable rollback.
Trace one connection from the client to the container
Trace the same destination port from the host, a client that should be allowed, and a client that should be denied. The host result alone is not enough.
| Check | Command or evidence | What it tells you |
|---|---|---|
| Published bind | docker ps, Compose file, docker inspect | Which host address and port Docker exposes |
| Host socket | ss -lntup | Whether a host process is listening, and on which address |
| Container address | docker inspect | The bridge IP and container port |
| Packet interface | ip -br address, packet capture if needed | Whether traffic arrives on public, private, or loopback interface |
| Forwarding rules | ufw show raw, iptables/nftables inspection | Which chain sees translated traffic |
| Upstream result | Cloud or router logs and policy | Whether traffic is filtered before Ubuntu |
| Application result | Container logs and local request | Whether the service accepts the connection |
Inspect the declaration first. A Compose entry like "127.0.0.1:8080:80" is materially different from "8080:80". Then compare the host socket with the container mapping:
sudo docker port CONTAINER_NAME
sudo docker inspect -f '{{json .NetworkSettings.Ports}}' CONTAINER_NAME
sudo ss -lntup | grep ':8080'
sudo docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' CONTAINER_NAMEReplace 8080 with the published port. If ss shows a wildcard Docker listener and an external client connects, the bind declaration already explains broad exposure. If there is no host listener but Docker’s published mapping exists, inspect Docker’s netfilter rules rather than assuming the application is unreachable.
For a live trace, capture the relevant traffic while making one test connection from the separate client:
sudo tcpdump -ni any 'tcp port 8080'If packets appear on the public interface, Ubuntu received them. If they never appear, inspect the AWS, Azure, Hetzner, router, load balancer, DNS target, and client-network controls. A cloud or upstream rule can permit or block traffic before Ubuntu sees it, so host counters alone do not prove Docker is responsible. If packets arrive and the container responds, compare the forwarding path and Docker chains with the UFW policy. Exact chain names and backend output vary by installation; identify the active path before writing a rule.
Apply the smallest fix for the exposure you actually want
Choose the correction from the intended audience for the service. Binding is usually the cleanest fix because it removes unwanted exposure from the Docker declaration.
- If only the host should use the service, bind the published port to loopback.
docker run -p 127.0.0.1:8080:80 IMAGE_NAMEFor Compose, use the equivalent declaration:
services:
app:
ports:
- "127.0.0.1:8080:80"Recreate the container through the normal deployment process. A host reverse proxy can then reach 127.0.0.1:8080, while remote clients cannot use that published port directly. Test from a separate network as well as from the host.
- If the service is needed on one private network, bind it to the host’s private address instead of 0.0.0.0.
services:
app:
ports:
- "PRIVATE_HOST_IP:8080:80"Use the address shown by ip -br address. A private bind is not automatically safe if that network is shared or routed broadly, so restrict the private network’s source ranges at the cloud, router, or host layer.
- If the service is public but should accept only a small source set, apply source filtering at the layer that sees the packet. A cloud security group or provider firewall can be the first control; UFW or a Docker-aware forwarding policy can provide defense in depth. These controls are separate from Docker’s host rules.
The DOCKER-USER chain is a commonly used hook for filtering Docker-forwarded traffic before Docker’s own later rules. A controlled policy can allow required source addresses and drop the rest, but the exact commands depend on interface names, Docker networks, IPv4 versus IPv6, and whether the system uses iptables-nft or native nftables. Preserve an SSH exception, test during a maintenance window, and manage the policy through the UFW integration or configuration-management method used by the installation.
Do not disable Docker’s networking rule management merely to make ufw status look complete. That can break published ports and bridge connectivity. If UFW must govern forwarded Docker traffic, use a documented, version-matched integration or a carefully reviewed DOCKER-USER/nftables policy. Stop and roll back if the allowed test client fails or the SSH session becomes unstable.
Verify the fix and confirm it survives redeployment
Test from both sides of the intended boundary. Use a separate allowed client for a service that should remain reachable and a separate denied client for a source that must be blocked.
| Test | Expected result |
|---|---|
| Host or approved private client | Service responds when access is intended |
| Unapproved remote client | Connection is refused or times out according to the policy |
| Public address from the host | Interpret carefully; local routing is not an external test |
| Container-local request | Application still works inside its intended network |
| IPv4 and IPv6 clients, if enabled | Both follow the intended exposure policy |
Record the result with nc or curl, and watch container logs during the test. Then restart the container and repeat. Recreate it from Compose or the deployment command, because a temporary rule or hand-edited container may disappear. After any planned Docker restart, repeat the tests. Also verify that UFW remains enabled and that the policy is loaded after an Ubuntu reboot.
sudo ufw status verbose
sudo docker compose config
sudo docker compose up -d
sudo docker ps --format 'table {{.Names}}\t{{.Ports}}'
curl --connect-timeout 5 http://SERVER_IP:8080Run curl from the appropriate client, not automatically from the server. For non-HTTP services, use the matching client or nc.
If the change causes trouble, restore the previous port mapping in the deployment declaration and recreate the container. If you added a dedicated firewall rule, remove that exact rule with the same managed tool that created it, after confirming the SSH allow rule remains. Do not flush chains. If remote access is lost, use the provider console or out-of-band recovery route and restore the saved configuration.
Escalate the investigation when packets never reach Ubuntu, IPv6 behaves differently, Docker’s displayed mappings conflict with socket output, or native nftables and compatibility iptables show different policies. Those symptoms point to an upstream firewall, another network namespace, an application proxy, or a backend-specific rule—not necessarily a UFW failure. Check the current official Docker, Ubuntu, UFW, and cloud-provider documentation for the installed versions before making backend-level changes.
Conclusion
Start with the published bind address and the client’s packet path. If the service should be local or private, change the deployment declaration to bind it to 127.0.0.1 or a specific private address; otherwise apply a narrowly scoped forwarding policy. Keep the SSH recovery path and repeat the tests after recreation, restart, and reboot.
Frequently Asked Questions
Why does ufw deny PORT not block my Docker container?
A published Docker port can be destination-translated and forwarded to the bridge, so the packet may not be governed by UFW’s ordinary INPUT rule. Check the published bind address and forwarding path first, then bind the port narrowly or apply a version-matched forwarding policy through a Docker-aware firewall hook.
What is the safest fix for a container that should only be used locally?
Bind the host side to 127.0.0.1, such as 127.0.0.1:8080:80, in the Docker or Compose declaration. Recreate the container and test from a genuinely separate network. A successful request from the host alone does not prove remote exposure.
Can I fix this by flushing iptables or disabling UFW?
No. Flushing rules can remove SSH access and Docker’s bridge networking, while disabling UFW hides the policy problem rather than narrowing exposure. Save the current state, preserve SSH, identify the active backend, and change only the specific bind or forwarding rule that controls the published port.
Sources
- How to Configure UFW with Docker on Ubuntu (Fix Docker Bypassing UFW) (oneuptime.com)
- UFW Docker Firewall Bypass Fix: 127.0.0.1 & DOCKER-USER (gearflowlab.com)