Can I get ip address inside my docker container?
As the IP address is in the first line of /etc/hosts, you can do in a container the awk command that prints the first word of the first line of /etc/hosts
$ awk 'END{print $1}' /etc/hosts
172.17.0.14
Why not something as simple as:
grep "`hostname`" /etc/hosts|awk '{print $1}'
or
grep "$HOSTNAME" /etc/hosts|awk '{print $1}'
I can find the IP address with
hostname -i
Of course, that may not be completely accurate if there is more than one interface.
Edit
Note: According to the hostname man page, hostname -i
uses DNS to resolve the ip address, where hostname -I
displays all the addresses except loopback, does not depend on DNS, and is recommended.
In all my Docker containers, -i
and -I
return the same results (but this is not the case on my desktop).