brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: the DOCKER-USER chain is where your own firewall rules for Docker go

Docker
~/tips/tip-docker-user-iptables-chain

sudo iptables -I DOCKER-USER -i eth0 ! -s 192.0.2.0/24 -j DROP

Restrict who may reach published container ports with rules Docker will not overwrite.

Docker manages its own iptables rules, and traffic to published ports is handled before the usual input rules see it. Rules you add to INPUT or through ufw do not stop it. Docker reserves one chain for you: DOCKER-USER.

Rules in DOCKER-USER are processed before Docker’s own forwarding rules, and Docker leaves them alone when it restarts or creates networks. The docs show this rule to allow only one subnet to reach published ports from the outside interface:

sudo iptables -I DOCKER-USER -i eth0 ! -s 192.0.2.0/24 -j DROP

Replace eth0 with your external interface and the subnet with the one you trust. The docs also suggest a rule that lets return traffic through, placed before any drop:

sudo iptables -I DOCKER-USER -m state --state RELATED,ESTABLISHED -j ACCEPT

Since -I inserts at the top, run that one last so it ends up first.

These rules are not saved by themselves. After a reboot they are gone unless you persist them, for example with iptables-persistent or a systemd unit that runs after Docker starts. A mistake here can lock out more than you intended, so I would test from another machine with a second session open. And if you only need a service on the host itself, publishing on 127.0.0.1 is simpler.

Further reading: Docker with iptables, Packet filtering and firewalls

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link