DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Join us tomorrow at 1 PM EST: "3-Step Approach to Comprehensive Runtime Application Security"
Save your seat
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Discovering Docker’s IP Address

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.

Tyler Jewell user avatar by
Tyler Jewell
·
Oct. 17, 16 · Tutorial
Like (2)
Save
Tweet
Share
7.45K Views

Join the DZone community and get the full member experience.

Join For Free

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
192.168.65.2
$ docker images
REPOSITORY          TAG    IMAGE ID     CREATED     SIZE
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 
    NETWORK_IF=eth0 
  fi
fi

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 
      NETWORK_IF=${i} 
    fi 
  done
fi

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 
    NETWORK_IF=”docker0" 
  fi
fi

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:

Getting Started With Docker

Docker (software) Network interface

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Best Practices for Writing Clean and Maintainable Code
  • 2023 Software Testing Trends: A Look Ahead at the Industry's Future
  • Do Not Forget About Testing!
  • AIOps Being Powered by Robotic Data Automation

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: