Skip to content

Instantly share code, notes, and snippets.

@slhck
Created October 23, 2024 09:23
Show Gist options
  • Save slhck/fb2750043a0cd776394b05130cc532c5 to your computer and use it in GitHub Desktop.
Save slhck/fb2750043a0cd776394b05130cc532c5 to your computer and use it in GitHub Desktop.
Fix Traefik container not being reachable after restarts
#!/usr/bin/env bash
#
# Update the ufw rules such that a Traefik container is reachable
set -e
for cmd in ufw-docker ufw docker; do
if ! command -v "$cmd" > /dev/null; then echo "Install $cmd first!"; exit 1; fi
done
disallowTraefik() {
ufw-docker delete allow traefik
ufw reload
}
allowTraefik() {
ufw-docker allow traefik
ufw reload
}
traefikName="traefik"
currentIp="$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $traefikName)"
echo "Traefik IP address: $currentIp"
echo "Current UFW rules:"
ufw status numbered | grep "allow traefik" || true
ipList="$(ufw status numbered | grep "allow traefik" | cut -d' ' -f 3 | sort | uniq)"
if [ -z "$ipList" ]; then
echo "No rules for traefik set up yet!"
allowTraefik
exit 0
fi
numIps="$(echo $ipList | wc -l)"
if [[ $numIps -ne 1 ]]; then
echo "More than one IP for traefik!"
exit 1
fi
if [[ $currentIp == $ipList ]]; then
echo "Current IP is up to date"
exit 0
else
echo "Current IP $currentIp is not the same as $ipList"!
disallowTraefik
allowTraefik
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment