Skip to content

Instantly share code, notes, and snippets.

@josephbolus
Forked from pedrolamas/docker-iptables-fix.sh
Last active August 9, 2024 02:59
Show Gist options
  • Save josephbolus/558b0ccf29d4b1ed0990779d5df7b2d1 to your computer and use it in GitHub Desktop.
Save josephbolus/558b0ccf29d4b1ed0990779d5df7b2d1 to your computer and use it in GitHub Desktop.
Forwarding real IP when running as a container by fixing Docker iptables on Synology NAS
#!/bin/bash
currentAttempt=0
totalAttempts=10
delay=15
while [ $currentAttempt -lt $totalAttempts ]
do
currentAttempt=$(( $currentAttempt + 1 ))
echo "Attempt $currentAttempt of $totalAttempts..."
result=$(iptables-save)
if [[ $result =~ "-A DOCKER -i docker0 -j RETURN" ]]; then
echo "Docker rules found! Modifying..."
iptables -t nat -A PREROUTING -p tcp --dport 80 -m addrtype --dst-type LOCAL -j DOCKER
iptables -t nat -A PREROUTING -p tcp --dport 443 -m addrtype --dst-type LOCAL -j DOCKER
iptables -t nat -A PREROUTING -p tcp --dport 53 -m addrtype --dst-type LOCAL -j DOCKER
iptables -t nat -A PREROUTING -p udp --dport 53 -m addrtype --dst-type LOCAL -j DOCKER
echo "Done!"
break
fi
echo "Docker rules not found! Sleeping for $delay seconds..."
sleep $delay
done
#!/bin/bash
sudo iptables -t nat -D PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
sudo iptables -t nat -D PREROUTING -m addrtype --dst-type LOCAL ! --dst 127.0.0.0/8 -j DOCKER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment