Skip to content

Instantly share code, notes, and snippets.

@kennedyj
Created January 16, 2014 22:53
Show Gist options
  • Save kennedyj/8465084 to your computer and use it in GitHub Desktop.
Save kennedyj/8465084 to your computer and use it in GitHub Desktop.
prune unused docker images
#!/bin/bash
# Get in use image tags
docker ps | grep -v 'IMAGE' | awk '{ print $2}' > tags-inuse.txt
# Get all images with tags, combine the image and tag keep the id
docker images -a | grep -v 'REPOSITORY' | grep -v '^<none>' | awk '{print $1":"$2"\t"$3}' > tags-full.txt
# Remove the in-use tags from the full list
grep -v -f tags-inuse.txt tags-full.txt > tags-prune-list.txt
while read l; do
echo "** removing image $l"
# Remove the tags from the images
echo "$l" | awk '{print $1}' | xargs docker rmi
# be kind
sleep 1
done < tags-prune-list.txt
echo "fin."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment