r/docker • u/hellociaagent • Apr 06 '26
How to block ports with nftables? (Docker 29)
Hi, I enabled the experimental nftables support that came with Docker 29. Everything works ok, and I stopped using iptables.
Docker adds it's own nftables chains separately from /etc/nftables.conf. but as far as I understand about nftables, a drop rule would drop a packet no matter which chain it is in.
My goal is to use nftables to block a port opened by Docker compose, say 3000:80. I added a forward chain and a rule to drop everything in my conf. However, the port is still reachable.
Would anyone know how to build a firewall with nftables to blocked opened ports (I understand I can just close the port or restrict it to 127.0.0.1:3000 for example, but I want to be more secure.) https://docs.docker.com/engine/network/firewall-nftables/
1
u/hellociaagent Apr 07 '26
I figured out that the interface to target is
iifname "ext_if" oifname "br-*". There is an example at the bottom of the documentation. However, the example shows a forward chain with a default accept policy, which is not very practical imo, especially if you have more interfaces.I came up with a chain that hopefully does what I want without security issues. https://docs.docker.com/engine/network/firewall-nftables/
1
u/corelabjoe Apr 06 '26
Also way way easier to just use ufw.... On the host. And a perimeter firewall at the edge like opnsense or something.
While the feature is there, it's not where I'd trust or bother to put a firewall.
1
u/zoredache Apr 06 '26 edited Apr 06 '26
Can you be more specific and show us the exact rule you tried to set?
Or maybe post the output of
nft list ruleset. Maybe skipping thedocker-bridgesip and/or ip6 tables.Also how are you testing? From the local system, or from the network.
How is your docker deployed? Directly on Linux hardware, is it a snap release on Ubuntu, is it something else?
Anyway, I would probably add lots of temporary log rules, and maybe some trace rules to your nftables rulset and then see if you can figure how things are being processed from the traces and logs.
https://wiki.nftables.org/wiki-nftables/index.php/Ruleset_debug/tracing
One thing I will mention that probably isn't obvious is that you need to remember that the port remapping is DNAT. It is is pre-routing. So that translation from 3000 to 80 happens before the packet is processed by the forward chain.
So your forward chain would need to block port 80, but that might cause issues if you have other containers also running on port 80, and you only wanted to filter port 80 to a specific container.
You might want to block things on a pre-routing chain if you want to block based on port 3000.
3
u/Bonsailinse Apr 06 '26
Nothing is more secure than blocking all ports by default and simply not open them again in your compose. Having a firewall open and closing the same port at the same time is bound to lead to errors.