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.
By default, the container is assigned an IP address for every Docker network it connects to. The IP address is assigned from the pool assigned to the network, so the Docker daemon effectively acts as a DHCP server for each container. Each network also has a default subnet mask and gateway.
Docker Containers with Public IPs¶ If you need to assign public routable IP addresses directly to each individual Docker containers, using routed networks will greatly simplify your configuration.
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).
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With