How to access localhost from inside a Docker Container in Linux

  1. Enable route_localnet for docker0 interface:
sysctl -w net.ipv4.conf.docker0.route_localnet=1
  1. Add this rules to iptables:
iptables -t nat -I PREROUTING -i docker0 -d -p all -j DNAT --to
iptables -t filter -I INPUT -i docker0 -d -p all -j ACCEPT
  1. You can now use to access localhost on the host.

reference https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach