Skip to content

Instantly share code, notes, and snippets.

@justin2061
Forked from rubot/Docker_UFW_IPTABLES.md
Created November 2, 2020 08:20
Show Gist options
  • Save justin2061/1a675faa191492ed1a1aa7fe66815b08 to your computer and use it in GitHub Desktop.
Save justin2061/1a675faa191492ed1a1aa7fe66815b08 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment