How to access localhost from inside a Docker Container in Linux
- Enable
route_localnet
fordocker0
interface:
sysctl -w net.ipv4.conf.docker0.route_localnet=1
- Add this rules to iptables:
iptables -t nat -I PREROUTING -i docker0 -d 172.17.0.1 -p all -j DNAT --to 127.0.0.1
iptables -t filter -I INPUT -i docker0 -d 127.0.0.1 -p all -j ACCEPT
- You can now use
172.17.0.1
to access localhost on the host.