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 container.
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 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
gg@805689be5f47:/$ awk 'END{print $1}' /etc/hosts 172.17.0.14
I can find the IP address with
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, wherehostname -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).You could also look for a line in /etc/hosts that ends with a container id and print the first field:
I'd use awk, but standard awk in dedian:jessie doesn't support regex quantifiers like
{12,}
.I found solution to my problem:
It's print something like: '172.17.0.135'
Why not something as simple as:
or
If you prefer to use
ip
rather thanifconfig
, on Linux you can do: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:
NOTE: This shows ALL of the IPs in the container. You can use
awk
orsed
to remove127.0.0.1/16
and other unwanted IPs. :)