Dynamically save and restore iptables rules for some chain(s)
iptables-dynamic parses the output of iptables-save to dynamically save and restore rules for some chain(s).
iptables
(Must have the commandsiptables-save
andiptables-restore
commands available)- At least, Python 2.7
pip install iptables-dynamic
iptables-dynamic --chains DOCKER SOME-OTHER-CHAIN --save
which would save them at /etc/iptables/rules.v4
One can restore those rules by
iptables-dynamic --restore
One may also use ip6tables-dynamic
for IPv6.
When there's an iptables service restart, all of the docker rules gets wiped out. The make the rules persist through a service restart, you may edit your iptables' systemd unit files to call below at ExecStopPre:
iptables-dynamic --chains DOCKER --save
and call below at ExecStartPost:
iptables-dynamic --restore
The copyright owner is Regents of the University of California and this script is published under a BSD 3-Clause license.
Developed at UCSB LSIT