Skip to content

Instantly share code, notes, and snippets.

@mathewdgardner
Last active February 24, 2016 22:07
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 mathewdgardner/11c94f04d875051353ed to your computer and use it in GitHub Desktop.
Save mathewdgardner/11c94f04d875051353ed to your computer and use it in GitHub Desktop.
#!/bin/bash
# Recursively go up ancestor directories looking for a docker-compose.yml file
function check() {
if [ -f "$1/docker-compose.yml" ]; then
compose_file="$1/docker-compose.yml"
echo "Using $compose_file for friendly names..."
elif [ "$1" = "/" ]; then
echo "Error: No docker-compose.yml file found" && exit 1
elif [ -d "$1" ]; then
check $(dirname $1)
fi
}
# Check for docker-compose file
check $(pwd)
# Find all the images in the docker-compose file and set the container ids as vars
# that contain the docker-compose names
for image in $(cat "$compose_file" | grep "^\w" | sed "s/://" | sort); do
prefix=$(basename $(dirname "$compose_file" | sed 's/-//'))
declare $image=$(docker ps | grep "$prefix"_"$image"_ | cut -d ' ' -f 1)
images+="$image "
done
# Infinite loop
while :; do
# Get stats from docker
stats=$(docker stats --no-stream)
# Check if there are any containers
# If there is only one line in $stats then it is just the labels
if [ $(wc -l <<< "$stats") -eq 1 ]; then
echo "Error: No containers found" && exit 1
fi
# Convert container ids into docker-compose names
for image in $images; do
if [ ! -z "${!image}" ]; then
stats=$(sed "s/${!image}/$image/" <<< "$stats")
fi
done
clear
# Format data into a table
column -t <<< "$stats"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment