Skip to content

Instantly share code, notes, and snippets.

@jimcadden
Last active October 17, 2017 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimcadden/3131c47089b596f3fd6029981214f2e7 to your computer and use it in GitHub Desktop.
Save jimcadden/3131c47089b596f3fd6029981214f2e7 to your computer and use it in GitHub Desktop.
Get container IPs of a Docker service
my_serv=$1
net_id=$(docker service inspect --format '{{.Endpoint.VirtualIPs}}' $my_serv | cut -c 3- | cut -d ' ' -f 1)
net_name=$(docker network inspect --format '{{.Name}}' $net_id)
docker service ps --format '{{.ID}} {{.Node}}' $my_serv |
while read serv_id node_id;
do
cont_id=$(docker inspect --format '{{.Status.ContainerStatus.ContainerID}}' $serv_id 2> /dev/null)
node_ip=$(docker node inspect $node_id --format '{{ .Status.Addr }}' 2> /dev/null)
cont_ip=$(docker -H $node_ip inspect --format '{{index .NetworkSettings.Networks "'$net_name'" "IPAddress"}}' $cont_id 2> /dev/null)
echo $cont_ip
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment