Last active
October 7, 2015 20:00
-
-
Save jarfil/c7d88c75b5dd1b8307b9 to your computer and use it in GitHub Desktop.
Docker: update + remove old unused images
This file contains 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
#!/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