Publishing a Docker port can make it reachable even when UFW says incoming traffic is denied. Docker routes published bridge-network traffic through NAT and forwarding rules; UFW’s host INPUT policy does not provide the access control you might expect.
This guide separates two jobs: limiting where a port is published, and filtering which clients can reach it. Binding to a LAN address reduces exposure, but does not make UFW’s host-input rules control forwarded container traffic.
1. Check the Existing Rules
Run on the Docker host with administrator access:
|
|
The rules below apply to Docker’s iptables backend, including iptables-nft compatibility mode. If Docker uses its native nftables backend, use the nftables guide instead. Do not create a fake DOCKER-USER chain to hide a backend mismatch.
2. Avoid Publishing Ports You Do Not Need
Services on the same Compose network can communicate by service name without ports. A database used only by an application normally needs no host-published port:
|
|
Create the password file securely before starting this example. Keep it out of Git. Do not replace an existing database deployment with this sample or change its PostgreSQL major version without a migration plan.
For a service used only by software on the host, bind to loopback:
|
|
For LAN clients, bind to an address actually assigned to the host:
|
|
A LAN binding is not a source allowlist. Routed clients may still reach it. Apply a forwarding policy when source restrictions are required, and check IPv6 exposure separately. Docker releases before 28.0.0 also have a documented localhost-publishing caveat for peers on the same layer-2 segment; update older installations.
3. Filter One Published Port with DOCKER-USER
This example protects TCP port 8080 published on 10.0.20.50, allowing new connections only from 10.0.10.0/24 arriving on ens18. Replace all three values before running it.
Use the incoming physical interface, not docker0. At this hook, destination NAT has already happened, so --ctorigdst and --ctorigdstport match the original host address and published port.
First save the existing IPv4 rules:
|
|
Add a dedicated chain without flushing any existing policy:
|
|
Run this setup once. If the chain exists, inspect it instead of running the commands again. The jump only handles new connections to this one published endpoint; other forwarding rules remain in force. Existing connections are unaffected.
This example covers IPv4 only. If the service is reachable through IPv6, configure and test an equivalent IPv6 policy or remove its IPv6 publication. Conntrack matching can have a performance cost.
4. Verify from Allowed and Blocked Clients
On the host:
|
|
From an allowed client and then a client outside the permitted subnet:
|
|
The allowed request should reach the application; the blocked request should time out. Use a new connection for each check. Verify container DNS, outbound access, and application health too. A host-local curl or an ss listing alone does not prove that external access is blocked.
5. Persistence and Rollback
The commands above are runtime rules. Integrate them into your existing firewall manager so they load after Docker creates DOCKER-USER, without duplicating rules. Verify again after reboot and after a Docker restart. Avoid blindly saving and replaying all of Docker’s dynamically generated rules.
To remove only this example:
|
|
Do not disable Docker’s iptables or ip6tables options as a UFW fix. Those options disable rules Docker needs for networking; they do not transfer container access control to UFW automatically.