Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get ip address inside my docker 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.

like image 706
Kroid Avatar asked Dec 27 '14 18:12

Kroid


People also ask

Does a Docker container have an IP address?

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.

Can Docker container have public IP?

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.


2 Answers

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

like image 138
Victor Roetman Avatar answered Sep 18 '22 05:09

Victor Roetman


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 
like image 29
user2915097 Avatar answered Sep 18 '22 05:09

user2915097