pc70
pc70

Reputation: 701

Get IP address of host where docker engine is running

From within a running docker container, is there a way to get IP address of the host on which the container is running?

Upvotes: 2

Views: 1815

Answers (2)

Haoming Zhang
Haoming Zhang

Reputation: 2842

larsks's answer is great. But if you need to get the public IP of host, you can try http://ifconfig.me/ . The command will be:

$ curl ifconfig.me

Then you can get the public IP of your host.

Upvotes: 0

larsks
larsks

Reputation: 311238

From within the container, the address of your default gateway is an address of the host. You can get that, for example, like this:

ip route | awk '$1 == "default" {print $3}'

That is your host's address on the bridge device that Docker created for your container network. You will be able to access any host services at that address that are either listening on all interfaces or that are are bound to that specific interface.

If you really need an external address of your host, you would need to pass that information in via an environment variable, like this:

docker run -e MY_IP=192.168.10.1 ...

Or via some other mechanism (configuration file, database value, etc).

Upvotes: 3

Related Questions