PortGate

Blueprints · Docker & Databases

Production 10 min · Docker / iptables

DOCKER-USER chain for published ports

Drops internet traffic to every published container port except the host ports you list, matched on the original port before Docker rewrites it. Survives reboots through a systemd unit. SSH and host services are not in this chain.

Live config

Baked-in protections

Step 1 · bash

See what Docker published

/root/portgate-blueprints/docker-ports.sh

Read-only. Every port in this list that is not in PUBLIC_PORTS stops answering from the internet after the next step.

#!/bin/sh
set -eu
docker ps --format '{{.Names}} {{.Ports}}' || echo "Docker is not running."
echo "Public interface: $(ip route get 1.1.1.1 | awk '{for (i=1;i<NF;i++) if ($i=="dev") print $(i+1)}')"

Step 2 · bash

Firewall script

/usr/local/sbin/docker-user-firewall.sh

Not run by the bundle. Check PUBLIC_IF and PUBLIC_PORTS, then run it once by hand and confirm your site still loads.

#!/bin/sh
set -eu
IF="eth0"
PUBLIC_PORTS="80 443"

iptables -N DOCKER-USER 2>/dev/null || true
iptables -F DOCKER-USER
iptables -A DOCKER-USER -m conntrack --ctstate ESTABLISHED,RELATED -j RETURN
iptables -A DOCKER-USER ! -i "$IF" -j RETURN
for port in $PUBLIC_PORTS; do
  iptables -A DOCKER-USER -i "$IF" -p tcp -m conntrack --ctorigdstport "$port" -j RETURN
done
iptables -A DOCKER-USER -i "$IF" -j DROP
iptables -A DOCKER-USER -j RETURN
iptables -L DOCKER-USER -n --line-numbers
echo "Published ports other than $PUBLIC_PORTS no longer answer on $IF. Host services and SSH are unchanged."

Step 3 · ini

Reapply after reboot

/etc/systemd/system/docker-user-firewall.service

A reboot clears iptables. This unit runs the script after Docker starts. Enable it with systemctl enable --now docker-user-firewall.service.

[Unit]
Description=Filter published Docker ports on the public interface
After=docker.service
Wants=docker.service

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/docker-user-firewall.sh
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target