Virtualization

How to Configure iptables, firewalld, and UFW with Docker Networks

Aseem Kishore
14 min read

The Back Room Tech is reader-supported. We may earn a commission when you buy through links on our site. Learn more.

A Docker container can stay reachable after you deny its published port in UFW or a firewalld host zone. Docker translates the destination and forwards the connection to the container. A rule for traffic delivered to the host may never see it. This guide traces that path, then shows two ways to close it: a DOCKER-USER rule for Docker’s iptables backend (which also covers firewalld hosts) and the community ufw-docker helper for UFW hosts. It tests each one from two machines outside the host.

The rule below applies to native Linux Docker Engine using bridge networking and Docker’s iptables firewall backend. Check the backend first; Docker’s nftables backend needs a different procedure. Docker explains the behavior in its packet filtering guide.

What Are Docker’s Firewall Chains?

For a published bridge-network port, Docker adds a NAT rule. It changes the destination from the host address and port to the container address and port. The packet then passes through FORWARD. With the iptables backend, Docker sends forwarded traffic through DOCKER-USER before its own forwarding rules. Docker documents DOCKER-USER as the place for an administrator’s filter rule.

NAT has already changed the destination when the packet reaches DOCKER-USER. If host port 8080 maps to container port 80, a normal destination-port match sees 80. A connection-tracking match can inspect the original host port instead. Docker’s iptables guidance explains the difference.

Later steps cover UFW hosts through the community ufw-docker helper, and how the same DOCKER-USER rule applies on firewalld hosts.

Prerequisites

  • An Ubuntu 24.04 Linux host (or another Ubuntu release supported by Docker Engine) with SSH or console access and a user that can run sudo. Commands in this guide were written for Ubuntu 24.04; check the official Docker Engine installation instructions to confirm the steps for your specific release.
  • Native Docker Engine with a bridge network and the iptables firewall backend.
  • iptables on the host and curl on two external test machines.
  • One source address or network to allow, plus a test machine outside that range.
  • The host’s reachable IPv4 address, incoming interface name, and a free published port.
  • Console access or an existing SSH session while changing firewall rules.
  • UFW or firewalld already installed and managing this host’s firewall, only if you plan to use the ufw-docker step (UFW) or follow the firewalld notes in Step 7.

The example publishes a temporary web container on host port 8080. Pick another port if it’s in use. The sample addresses are documentation addresses. Replace them with addresses from your network before running the variable assignments. A NAS drive, Raspberry Pi, or another Linux system can be an external test client if it has a different source address.

Step-by-Step Guide

Step 1: Confirm Docker and its firewall backend

On Ubuntu 24.04 (or another Ubuntu release supported by Docker Engine), install the tools used here if they’re missing:

sudo apt update
sudo apt install -y iptables curl
sudo docker version --format '{{.Server.Version}}'

The last command should print your installed Docker Engine version. If Docker is absent, follow the Ubuntu Docker Engine installation guide before continuing.

Check Docker’s daemon settings and the DOCKER-USER chain:

sudo cat /etc/docker/daemon.json
sudo systemctl cat docker
sudo iptables -L DOCKER-USER -n -v

daemon.json may not exist; that alone is fine. Look for firewall-backend set to nftables in the daemon configuration or service arguments. With Docker’s iptables backend, the last command should show a Chain DOCKER-USER heading, even if the chain is empty. Stop this rule procedure if Docker is configured for the nftables backend.

Step 2: Publish a test port and identify the incoming interface

Display the host’s IPv4 interfaces:

ip -br -4 address

Find the interface that receives outside connections and its reachable address. Edit these assignments before pasting them into the host shell. ALLOW_CIDR can be one address with /32 or a trusted network range.

EXT_IF=eth0
HOST_IP=198.51.100.10
ALLOW_CIDR=203.0.113.0/24
HOST_PORT=8080

Start a temporary container and publish its port:

# -d runs the container in the background; -p maps host port 8080 to container port 80.
sudo docker run -d --name firewall-demo -p "${HOST_PORT}:80" nginx:alpine
sudo docker ps --filter name=firewall-demo

Expect docker ps to show a mapping such as 0.0.0.0:8080->80/tcp. Docker may also show an IPv6 mapping. Check that separately before treating the service as restricted.

docker ps showing the firewall-demo container and its published host port mapped to container port 80

Step 3: Trace the published connection through Docker’s rules

Inspect the forwarding chains, Docker’s administrator chain, and the NAT table:

sudo iptables -L FORWARD -n -v
sudo iptables -L DOCKER-USER -n -v
sudo iptables -t nat -L -n -v

Look for Docker’s jump from FORWARD toward DOCKER-USER and a NAT rule for the published port. The pkts and bytes counters help show whether a later test reaches a rule. Chain names and nearby rules vary by Docker version and host setup.

This path explains why UFW can appear to miss the connection. Docker translates and forwards it toward the container. A typical ufw deny 8080/tcp rule covers traffic on the host’s INPUT path. A firewalld restriction on the host’s zone can miss the forwarded path too. Neither host rule proves the container port is closed.

iptables FORWARD and DOCKER-USER chains with packet counters, plus the NAT rule for the published port

Step 4: Inspect UFW or firewalld without changing their rules

If you use UFW, check its status:

sudo ufw status verbose

A deny entry for 8080/tcp may appear even while the Docker container remains reachable.

If you use firewalld, inspect its active zones and Docker’s integration objects:

sudo firewall-cmd --get-active-zones
sudo firewall-cmd --zone=docker --list-all
sudo firewall-cmd --get-policies

When Docker integrates with firewalld, it creates a docker zone for bridge interfaces. It also creates a docker-forwarding policy that permits forwarding from other zones to that zone. The exact inspection command and output for viewing a policy’s details can vary by firewalld version; if --info-policy isn’t available on your system, use --get-policies to confirm the policy name, then consult your firewalld version’s documentation for the matching detail command.

These objects help explain why a host-zone port restriction may miss a published container port. Inspecting the zone and policy does not restrict the port. Their presence and displayed fields depend on the running setup. Step 7 covers how to restrict the port on a firewalld host.

firewalld active zones, the docker zone, and the docker-forwarding policy on a host where Docker created them

Step 5: Restrict the published port in DOCKER-USER (Docker’s iptables backend)

This rule drops incoming TCP connections to the original host port from sources outside the allowed range. It applies only to the chosen incoming interface and port. Check the four variable values from Step 2 first.

Warning: A wrong port, interface, or source range can cut off access. Keep console access available. The rule takes effect immediately.

sudo iptables -I DOCKER-USER 1 -i "$EXT_IF" -p tcp -m conntrack --ctorigdstport "$HOST_PORT" ! -s "$ALLOW_CIDR" -j DROP
sudo iptables -L DOCKER-USER -n -v --line-numbers

Expect a DROP rule at line 1 in DOCKER-USER. -I ... 1 puts it before later rules. --ctorigdstport matches the port the client requested before Docker changed the destination to the container’s port. Docker warns that original-destination connection-tracking matches can affect performance. Use this focused match when the published host port matters.

If the host publishes the same port on more than one address, add --ctorigdst "$HOST_IP" to target one original destination address. Review every published address before narrowing the rule.

Step 6: Restrict the published port with ufw-docker (alternative for UFW hosts)

If this host manages its firewall through UFW and you’d rather work with UFW’s own commands than raw iptables, the community-maintained ufw-docker project patches UFW so it can filter Docker’s forwarded traffic. ufw-docker is not part of UFW or Docker; it edits UFW’s after.rules file, so re-run its install step after any UFW package upgrade that replaces that file, and review the patch before relying on it in production.

Install the helper script and apply its integration:

# Pinned to the 251123 release tag (November 23, 2025) instead of the moving master branch;
# check https://github.com/chaifeng/ufw-docker/releases for a newer tag and read the script before running it as root
sudo wget -O /usr/local/bin/ufw-docker https://github.com/chaifeng/ufw-docker/raw/251123/ufw-docker
sudo chmod +x /usr/local/bin/ufw-docker
sudo ufw-docker install
sudo systemctl restart ufw

ufw-docker install adds a Docker-aware section to UFW’s forwarding rules and reloads UFW. Confirm UFW is active with sudo ufw status before and after this step.

After ufw-docker install, published container ports are closed to outside networks until you allow them. Pick one of these, using the container’s listening port (80 here), not the host’s published port, because the rules match traffic after Docker’s NAT step.

To restrict the port to the trusted range from Step 2, which is the goal of this guide, add a route rule with that source, following the ufw route allow proto tcp from ... to any port 80 pattern in the ufw-docker README:

sudo ufw route allow proto tcp from "$ALLOW_CIDR" to any port 80
sudo ufw status numbered

Only if the service should be reachable from every source, use the helper’s container-level allow instead. It opens the port to everyone, so don’t combine it with the scoped rule above:

sudo ufw-docker allow firewall-demo 80/tcp

The scoped route rule matches any published container whose container port is 80; the README shows how to target a single container by its IP address if several containers listen on 80.

Rule syntax and installation steps have changed between ufw-docker releases. Check the project’s current README against your installed UFW and Docker versions before using it on a production host, and test from the allowed and blocked machines in Step 8 to confirm the combined rule set behaves as expected.

Step 7: On firewalld hosts, restrict with DOCKER-USER (alternative path for firewalld hosts)

Docker’s firewalld integration is part of its iptables/ip6tables rule management, per Docker’s packet filtering docs; it is not a procedure for the nftables backend, which this guide doesn’t cover. Docker documents the docker zone and the docker-forwarding policy (which allows forwarding from any zone to the docker zone), but it doesn’t document a zone or policy rule that restricts a published port by source.

Traffic reaching the docker zone has already been through Docker’s DNAT, so a rich rule written for the host’s published port isn’t a reliable control, and this guide doesn’t present one as the fix.

With Docker’s iptables backend, the DOCKER-USER chain still exists on a firewalld host. Confirm it with the Step 1 check, then apply the Step 5 rule unchanged and verify it with the outside tests in Step 8:

sudo iptables -L DOCKER-USER -n -v --line-numbers

Depending on firewalld’s version and backend, a firewall-cmd --reload can remove rules added with plain iptables. Re-run the listing above after any reload, and make the rule persistent with your distribution’s supported method before you rely on it.

Step 8: Prove access from allowed and blocked external sources

On a machine whose source address is inside ALLOW_CIDR, run this command. Replace the sample address with the Docker host’s reachable address:

curl --connect-timeout 5 --max-time 8 -I http://198.51.100.10:8080/

For the example web container, expect an HTTP response such as HTTP/1.1 200 OK.

Run the same command on a second machine whose source address is outside ALLOW_CIDR:

curl --connect-timeout 5 --max-time 8 -I http://198.51.100.10:8080/

With a DROP rule the blocked client usually times out. A refusal or an HTTP response means something else answered or the rule didn’t match (IPv6, another firewall, or an existing connection), so check the evidence for your method: the DOCKER-USER DROP rule’s packet counter (Steps 5 and 7) or sudo ufw status verbose together with ufw-docker’s rule listing (Step 6).

sudo iptables -L DOCKER-USER -n -v --line-numbers

The DROP rule’s packet counter should rise when the blocked machine retries. A test from the Docker host alone proves little here; local traffic can take a different path.

Configuration

Setting or patternWhat to check
Allowed sourceUse one IPv4 address with /32 or the smallest trusted network range that fits the service.
Published host port--ctorigdstport matches the port the client requested, even when the container listens on another port.
Incoming interface-i limits the rule to traffic arriving on the chosen interface. Check hosts with more than one external interface.
Rule lifetimeThe command changes the running iptables rules. After the external tests pass, arrange persistence through your distribution’s firewall management method. iptables-save alone does not install a boot-time restore service.
Docker firewall backendDOCKER-USER applies to Docker’s iptables backend. Docker’s nftables backend guidance calls for administrator-owned nftables chains instead.

Choose DOCKER-USER (including on firewalld hosts) or ufw-docker based on which firewall tool already manages this host; running both on the same host adds complexity without added protection.

A Compose service with a published ports entry also uses Docker’s port publishing. Check its host-port mapping with docker ps, then apply the rule to that port. Review the rule and retest if container addresses or published ports change.

IPv6 needs its own check. An IPv4 iptables rule does not show whether an IPv6 published address is restricted. Check docker ps for IPv6 publication, then test the host’s IPv6 address from outside. Rootless Docker has a different network path too. Confirm its listener and firewall path before using this rootful Engine procedure.

Tips and Troubleshooting

UFW says “DENY,” but the container still responds

Cause: Docker handles the published connection through NAT and forwarding rules. The ordinary host INPUT rule you expected to apply may miss it.

Fix: Check the mapping with docker ps. Inspect NAT and DOCKER-USER as in Step 3, then apply and test the Step 5 rule from outside, or use the ufw-docker procedure in Step 6 if you’d rather manage this through UFW. Check ufw-docker’s current documentation against your installed UFW and Docker versions before deploying it on a production host.

An appended rule at the end of FORWARD may be bypassed

Cause: Docker’s own chains run before any rule you append to the end of FORWARD. If an earlier Docker rule already accepts or rejects the packet, your appended rule never sees it. Traffic that isn’t accepted or rejected by an earlier rule can still reach an appended FORWARD rule.

Fix: Put the restriction in DOCKER-USER, which runs before Docker’s own forwarding rules and before traffic can be resolved earlier in the chain. Make a fresh connection from the blocked external source, then check the rule’s packet counter.

A rule for host port 8080 never matches

Cause: Docker changes the destination to the container address and port before traffic reaches DOCKER-USER.

Fix: Match the container-side port, or use -m conntrack --ctorigdstport 8080 for the original host port. The example uses the original host port.

The DOCKER-USER chain is missing

Cause: The host may use Docker’s nftables backend, rootless Docker, or another firewall setup.

Fix: Recheck /etc/docker/daemon.json, Docker’s service arguments, and the published port’s path. Docker does not create DOCKER-USER for its nftables backend. Use Docker’s nftables documentation to plan an administrator-owned chain, The procedures in Steps 5 through 7 assume Docker’s iptables backend and don’t apply to the nftables backend.

The blocked test still connects

Cause: The test may use another interface or IPv6. It may also come from inside the allowed range or target another published port.

Fix: Check the source address seen by the host, the mapping in docker ps, and the DROP counter or equivalent rule hit for whichever method you applied. Correct the rule, then test with a new external connection. A firewalld docker zone listing alone does not close the port; apply the DOCKER-USER rule (Steps 5 and 7) and retest.

Wrapping Up

With Docker’s iptables backend, DOCKER-USER lets you restrict forwarded container traffic. That includes firewalld-managed hosts. On UFW-managed hosts, ufw-docker patches UFW’s forwarding rules so UFW commands can do the job; it still works through netfilter rules on its own path, so use one method, not both. Keep the outside tests in the job: they show whether the trusted client connects and the blocked client stops. Then arrange rule persistence through your distribution’s firewall management method.

StepActionApplies To
InspectCheck the published port, NAT rule, and forwarding countersNative Linux Docker Engine
Restrict (iptables)Add a scoped DOCKER-USER ruleDocker iptables backend
Restrict (UFW)Install ufw-docker and scope its allow rule to a trusted sourceUFW-managed hosts
Restrict (firewalld)Apply the Step 5 DOCKER-USER rule and re-check it after firewalld reloadsHosts with Docker’s firewalld integration active
VerifyTest from allowed and blocked external sourcesEvery published service