Skip to content

Instantly share code, notes, and snippets.

@jarfil
Last active October 7, 2015 20:00
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 jarfil/c7d88c75b5dd1b8307b9 to your computer and use it in GitHub Desktop.
Save jarfil/c7d88c75b5dd1b8307b9 to your computer and use it in GitHub Desktop.
Docker: update + remove old unused images
#!/bin/bash
# for every named image
docker images | sed "1d" | cut -d " " -f1 | sort -n | uniq | grep -v '^<none>$' |
while read f ; do
# pull latest image, skip pruning if can't pull
if ! docker pull "$f" ; then continue ; fi
# get latest image id
IMAGE_ID=`docker images "$f" | sed "1d;2q" | sed 's/^[^ ]* *[^ ]* *//;s/ .*$//'`
if [ "x$IMAGE_ID" == "x" ] ; then continue ; fi
# remove all except latest and IMAGE_ID
docker images "$f" | sed "1d" | grep -v " latest " | sed 's/^[^ ]* *[^ ]* *//;s/ .*$//' | sort | uniq | grep -v "$IMAGE_ID" |
while read g ; do
echo docker rmi $g
done
done
# remove all "unnamed && unused"
docker images -a | grep '^<none>' | sed "s/^<none> *<none> *//;s/ .*//" | xargs -n1 docker rmi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment