DEV Community

Cover image for Find IP address of Docker container
Adam K Dean
Adam K Dean

Posted on

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.

Top comments (0)