Skip to content

Instantly share code, notes, and snippets.

@lavie
Last active April 24, 2017 23:57
Show Gist options
  • Save lavie/0e92ee72ccb3b8a78534 to your computer and use it in GitHub Desktop.
Save lavie/0e92ee72ccb3b8a78534 to your computer and use it in GitHub Desktop.
Free disk space by deleting "stale" docker images (keep around the latest N tags of each image)
#!/bin/bash
images=$(./stale_image_tags.sh my.repo 3)
for image in $images; do
docker rmi $image
done
#!/bin/bash
if [ "$#" -ne 2 ]; then
cat <<EOF
Usage: stale_image_tags.sh <image prefix> <keep count>
image prefix: e.g. my.repo
keep count: how many most recent tags to excluse for each image
Prints all image:tag pairs except the most recent <keep count> for each image.
EOF
exit 1
fi
prefix=$1
keep_count=$(($2 + 1))
base_images=$(docker images | grep -e "^$prefix" | awk ' {print $1 }' | sort | uniq )
for image in $base_images; do
docker images | grep $image | tail -n +$keep_count | awk '{print $1":"$2}'
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment