NOTE: This post applies to Docker < 17.06
By default docker leaves network ports wide open to the world. It is upto you as the sysadmin to lock these down. Ideally you would have a firewall somewhere upstream between your host and the Internet where you can lock down access. However, in a lot of cases you have to do the firewalling on the same host that runs docker. Unfortunately, Docker makes it tricky to create custom iptables rules that take precedence over the allow-all ruleset that Docker introduces. There is a pull request that promises to help in this regard.
Until the fix is available, [EDIT: fixed in 17.06] the way I work around this problem is as follows:
Create a systemd service that runs my custom rules after the Docker service starts/restarts -
[Unit] Description=Supplementary Docker Firewall Rules After=docker.service Requires=docker.service PartOf=docker.service [Service] Type=oneshot RemainAfterExit=yes ExecStart=/usr/local/bin/docker-firewall.sh [Install] WantedBy=multi-user.target
/usr/local/bin/docker-firewall.sh is a shell script which simply inserts IPTables rules at the top of the ‘DOCKER’ chain:
#!/bin/bash # # called by docker-firewall.service # # Work around for controlling access to docker ports until PR#1675 is merged # https://github.com/docker/libnetwork/pull/1675 # IPTABLES=/usr/sbin/iptables CHAIN="DOCKER" $IPTABLES -I $CHAIN -i eth0 -j RETURN $IPTABLES -I $CHAIN -i eth0 -s <trusted IP> -j ACCEPT $IPTABLES -I $CHAIN -i eth0 -p tcp -m multiport --dport <port list> -j ACCEPT
- you should modify these rules to suit. Put only those ports that you want open to the world in the
<port list>, and any trusted IPs that should have access to all ports in the
- the rules are specific in reverse order, as they are being inserted at the top of the chain. You could instead specify the insert index (e.g. 0,1,2).
- make sure the shell script is executable (
- I’ve chosen to RETURN Internet traffic that doesn’t match the first 2 rules, but you may choose to simply DROP that traffic there. Either way, the last rule must take final action on Internet traffic to ensure that subsequent Docker rules don’t allow it in!!
Once those files have been created, you can enable the service with
systemctl enable docker-firewall. Now when you restart Docker (or upon reboot), this service will run afterwards and you’ll see your custom rules appear at the top of the DOCKER chain in iptables.