How to get container ip address inside this container?
\'docker inspect $hostname ...\' not suitable, because I don\'t share /var/run/docker.sock host file to contai
Normally you can use the linux program ifconfig
to get IP addresses and other networking details. Your container may not have it, in which case you'll need to install it via apt-get
or yum
or your distro's package manager. A basic pipeline to get the IP address would be
ifconfig eth0 | grep "inet addr:" | cut -d : -f 2 | cut -d " " -f 1
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
If you prefer to use ip
rather than ifconfig
, on Linux you can do:
ip addr | grep inet | tr -s ' ' | cut -d ' ' -f 3
This simply gets all of the IP addresses and interfaces, searches only for those starting with inet
and returns only the 3rd column.
As a nice side-effect, this includes the subnet mask, too! (e.g. 172.17.0.2/16
)
If you don't want the subnet mask, you can use:
ip addr | grep inet | tr -s ' ' | cut -d ' ' -f 3 | tr -d '/'
NOTE: This shows ALL of the IPs in the container. You can use awk
or sed
to remove 127.0.0.1/16
and other unwanted IPs. :)