Discovering Docker’s IP Address

DZone 's Guide to

Discovering Docker’s IP Address

Want to interact with your Docker daemon? You're going to need its IP address. See how you can find it using Eclipse Che.

· Cloud Zone ·
Free Resource

With Eclipse Che, we write a lot of utilities using Docker containers. They are small in footprint and portable that can run on many different operating systems without the user having to install additional software.

Many of our utilities need to also create and manage Docker containers, so while our code is running in a Docker container, we need to interact with the Docker daemon that is managing us. This means that we’ll need its IP address.

Docker comes in many different flavors — boot2docker, Docker for Windows / Mac, and native for Linux.

You can test the utility with docker run --net=host codenvy/che-ip. This will download the Eclipse Che IP utility. It is about 4.8 MB in size.

$ docker run — net=host codenvy/che-ip
$ docker images
codenvy/che-ip      latest a6bbac2f56dc 5 weeks ago 4.8 MB

The Dockerfile is simple — it adds a shell script and runs it as part of the command.

FROM alpine:3.4 
COPY getip.sh /bin/getip.sh 
CMD [“/bin/getip.sh”]

The script that gets executed is a simple script. It first determines the network interface that docker is bound to, and then uses that network interface to grab an IP address. Given a particular network interface, the Docker container uses the ip utility to grab the host’s IP address (note that --net=host is what allows us to use the host’s network configuration instead of the container’s):

ip a show “${NETWORK_IF}” | grep ‘inet' | cut -d/ -f1 | awk ‘{print $2}'

Different implementations of Docker bind themselves to different network interfaces. Some are predictable and others are not. So, we need to determine the appropriate network interface to use based upon the underlying host’s configuration.

If the installation is Docker for Windows or Docker for Mac, then we are guaranteed that Docker is running on eth0.

if uname -r | grep -q ‘moby’; then 
  if [ -d “/sys/class/net/eth0” ]; then 

Otherwise, the inspection is a bit trickier. In this case, Docker could be bound to different Ethernet interfaces. We know that Docker is running in a non-hypervisor VM, so we do a search on /sys/class/net and then iterate through all of the returned values to find the first interface bound to eth then this is the Docker network interface. We’ll use that.

# If the NETWORK_IF has not been set, then search for it
if test -z ${NETWORK_IF}; then 
  for i in $(ls /sys/class/net); do 
    if [ ${i:0:3} = eth ]; then 

Otherwise, if neither of these functions returns a valid network interface, then it is because that some Linux distrubtions are providing specialized names to their interfaces, like ensp0s3 for CentOS. In this situation, Docker is typically bound to the docker0 interface.

# If the NETWORK_IF still not set, then search for docker0
if test -z ${NETWORK_IF} then 
  if [ -d “/sys/class/net/docker0” ]; then 

Otherwise, if this test still fails, then it is not possible to determine the current interface. For our utilities that search for Docker’s IP, when we get back an exit value of 1 then we interact with the admin to learn more.

Great work by Florent Benoit, on building this utility. The source code for the Dockerfile and associated scripts are on Che’s GitHub repository.

Related Refcard:

cloud, daemon, docker, eclipse

Published at DZone with permission of Tyler Jewell , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}