Skip to content

Instantly share code, notes, and snippets.

@Gbps
Forked from menxit/Docker_UFW_IPTABLES.md
Last active August 23, 2018 05:38
Show Gist options
  • Save Gbps/2f6df2325b7f7f705e164d73d5ef3ca4 to your computer and use it in GitHub Desktop.
Save Gbps/2f6df2325b7f7f705e164d73d5ef3ca4 to your computer and use it in GitHub Desktop.
Docker UFW and iptables
  1. Disable iptables
$ sudo vim /etc/docker/daemon.json
{
  "iptables": false
}
  1. You have to set Docker's bridge postrouting in ufw:
$ ifconfig docker0

It should returns something like this:

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255

So docker0's network is 172.17.0.1/16

sudo vim /etc/ufw/after.rules
*nat
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING -s 172.17.0.1/16 -o eth0 -j MASQUERADE
COMMIT
  1. Finally you have to restart UFW and Docker
sudo ufw disable
sudo ufw enable
sudo systemctl restart docker
  1. Also, ufw will keep blocking unless you allow forwarding
sudo sed -i -e 's/DEFAULT_FORWARD_POLICY="DROP"/DEFAULT_FORWARD_POLICY="ACCEPT"/g' /etc/default/ufw
sudo ufw reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment