Accessing localhost inside the container itself

Hi,

This is my first post and my English is not that good, so pls do not BASH me…

I think you miss the point of “localhost”. As it always points to the actual LOCAL “machine” where it is called, it will try to curl the 8080 port of the container itself this way.

If you execute docker inspect test you can find a line like this “Gateway”: “172.17.0.1” this points to the base Docker network’s Gateway (represented by docker0 if you execute ifconfig).

So running docker exec test curl http://172.17.0.1:8080 will give you the page you are hosting on the machine where you run Docker.

And if you want to go deeper you can write a script like this inside the container:

#!/bin/bash
curl `route | awk ‘/^default/ { print $2 }’`:8080

Which will give you the same result if you run docker exec test ./path/of/script/script.sh from the host machine, except it will work with any IP, as route | awk ‘/^default/ { print $2 }’ translates to the default Gateway’s IP address.

I hope this helps…

1 Like