Find IP address of Docker container

adamkdean

Adam K Dean

Posted on May 28, 2014

Find IP address of Docker container

To get the IP address of a Docker container, use the following command:

$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${CID}
Enter fullscreen mode Exit fullscreen mode

So let's say the ID of the container is a2150s, we could get the IP like so:

$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' a2150s

172.17.0.2
Enter fullscreen mode Exit fullscreen mode

I have written a script for this, /usr/local/bin/dockerip:

#!/bin/bash

COUNT=`docker ps | grep $1 | wc -l | bc`

if [ $COUNT -gt 0 ]; then
  IP_ADDRESS=`docker inspect --format '{{ .NetworkSettings.IPAddress }}' $1`
  echo "$1: $IP_ADDRESS"
else
  echo "Could not find $1. Check container is running."
fi
Enter fullscreen mode Exit fullscreen mode

This will form part of a repository of useful docker scripts.

πŸ’– πŸ’ͺ πŸ™… 🚩
adamkdean
Adam K Dean

Posted on May 28, 2014

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related