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
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. :)