From 0c1efb0e640b89d4d690dcb646e8acccb276e681 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Thu, 15 Feb 2018 22:53:35 +0100 Subject: [PATCH] Improve docker-clean to avoid "in use" errors Signed-off-by: Knut Ahlers --- bin/docker-clean | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/bin/docker-clean b/bin/docker-clean index b1bc988..35684a5 100755 --- a/bin/docker-clean +++ b/bin/docker-clean @@ -11,10 +11,13 @@ step "Removing containers created / exited >= ~1h ago..." CONTAINERS=$(docker ps -a | awk '/(hours?|days?|weeks?|months?) ago\s+(Created|Exited)/{ print $1 }' | xargs) [ -n "${CONTAINERS}" ] && docker rm ${CONTAINERS} -step "Removing images missing repository or tag..." -IMGS=$(docker images | awk '/.*(hours?|days?|weeks?|months?) ago/{ print $3 }' | xargs) -[ -n "${IMGS}" ] && docker rmi ${IMGS} +step "Untagging all images not used by containers..." +PS_OUTPUT=$(docker ps -a) +for img in $(docker images | awk '/^[a-z].*(hours?|days?|weeks?|months?) ago/{ print $1":"$2 }' | grep -v '' | sort | uniq); do + if ! ( echo "${PS_OUTPUT}" | grep -q "\s${img}\s" ); then + docker rmi --no-prune "${img}" + fi +done -step "Removing images with tags not being used (used images will throw errors, that's normal)..." -IMGS=$(docker images | awk '/(hours?|days?|weeks?|months?) ago/{ print $1":"$2 }' | xargs) -[ -n "${IMGS}" ] && docker rmi ${IMGS} || true +step "Removing dangling images..." +docker image prune -f