Created
March 10, 2021 09:51
-
-
Save tuergeist/08a2f45306d78eadba017f2b114dd58d to your computer and use it in GitHub Desktop.
Get docker container IP addresses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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