Skip to content

Instantly share code, notes, and snippets.

@tuergeist
Created March 10, 2021 09:51
Show Gist options
  • Save tuergeist/08a2f45306d78eadba017f2b114dd58d to your computer and use it in GitHub Desktop.
Save tuergeist/08a2f45306d78eadba017f2b114dd58d to your computer and use it in GitHub Desktop.
Get docker container IP addresses
#!/usr/bin/env bash
# Docker Container IP
# Usage:
# get_container_ips.sh
# Prints out all IPs for running containers
#
# get_container_ips.sh <ID|name>
# Prints out the IP for a given container or ID
# https://docs.docker.com/engine/reference/commandline/ps/#formatting
function get_ip() {
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $1
}
if [ -z "$1" ] ; then
# list
for container in $(docker ps --format "{{.ID}};{{.Names}}") ; do
cid=$(echo "$container" | cut -d ';' -f1)
name=$(echo "$container" | cut -d ';' -f2)
echo -n "$name = "
get_ip $cid
done
else
get_ip $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment