VPS nftables Firewall With Docker
VPS nftables Firewall With Docker
This note records a small nftables firewall setup for a
VPS that also runs Docker.
The goal is not to write a clever universal ruleset. The goal is
simpler: keep the host input path small, expose only the
ports that should be public, and avoid fighting Docker's own NAT
rules.
The example uses documentation IP addresses:
203.0.113.10as the admin's trusted IP198.51.100.20as the server IP22222as the SSH port
Replace them with your own values.
Basic Rules
Use the inet family so the same table can handle IPv4
and IPv6:
1 | #!/usr/sbin/nft -f |
Docker Notes
Docker manages its own forwarding and NAT rules. If you blindly flush
everything and then set forward to drop,
containers may lose network access or published ports may stop
working.
For a small VPS, I usually keep the boundary simple:
- protect the host through the
inputchain - let Docker manage container NAT
- expose public services through Caddy on
80/443 - bind internal app ports to
127.0.0.1when possible
Example Docker port mapping:
1 | ports: |
Then let Caddy publish it over HTTPS.
Apply Safely
Validate before loading:
1 | sudo nft -c -f /etc/nftables.conf |