ryoakg
7/8/2016 - 5:24 AM

docker-ip-show.sh

#!/bin/sh
DOCKER='docker'
THIS=`basename $0`

if [ x'--help' = x"$1" ] ;then
  echo "Usage:"
  echo "  $THIS <CONTAINER-ID|CONTAINER-NAME> :show specific container."
  echo "  $THIS                               :show all containers."
  echo "  $THIS --help                        :print this message."
  echo
  echo "Available containers:"
  printf "%-12s\t%s\n" ID Names
  ${DOCKER} ps --format "{{.ID}}\t{{.Names}}"
  exit 1
fi

# show all containers
if [ -z "$1" ] ;then
  printf "%-12s\t%-15s\t%s\n" "ID" "IP" "Name"
  for id in `${DOCKER} ps --format "{{.ID}}"` ;do
    ip=`${DOCKER} inspect --format '{{ .NetworkSettings.IPAddress }}' $id`
    name=`${DOCKER} inspect --format '{{ .Name }}' $id`
    printf "%-12s\t%-15s\t%s\n" $id $ip $name
  done
  exit 0
fi

# show specific container
${DOCKER} inspect --format '{{ .NetworkSettings.IPAddress }}' $1