Skip to content

Instantly share code, notes, and snippets.

@mrclay
Last active April 22, 2024 14:36
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save mrclay/da1a83380371ee15ae02b4bcc3db8da1 to your computer and use it in GitHub Desktop.
Save mrclay/da1a83380371ee15ae02b4bcc3db8da1 to your computer and use it in GitHub Desktop.
Flush IP tables and restart docker
#!/bin/bash
# Script is needed because my default firewall rules are messed up and after
# every restart, docker containers can't make connections to the host, notably
# preventing debuggers like xdebug from attaching.
# If networking fails in your containers but works in others, rm and re-create the
# docker network that container is bound to.
set -euo pipefail
# Unless docker is stopped with no containers running, docker will leave zombie
# proxy processes that hold the ports open preventing the start of new containers.
# If this happens I have to kill them manually: https://stackoverflow.com/a/61239636/3779
if [ ! $(docker ps | wc -l) == "1" ]; then
echo "Some docker containers are running."
exit 0
fi
sudo service docker stop
sudo iptables -P INPUT ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -P OUTPUT ACCEPT
sudo iptables -t nat -F
sudo iptables -t mangle -F
sudo iptables -F
sudo iptables -X
sudo service docker start
@justineuro
Copy link

Hi Steve,

Many thanks for this very useful script! Indeed this allowed my local docker install to run certain applications that depend on a properly built firewall.

I had to add a # right before the first line so that it becomes #!/bin/bash. Other than that, everything seemed fine. Kudos! 🎉

Best,
Justine Leon

@sazeygit
Copy link

sazeygit commented Feb 2, 2024

Saved me a bunch of hassle, many thanks!

@remoteweb
Copy link

🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment