From 885a8c553dc91b18513737344db1095ed904eabf Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sun, 8 Jul 2018 21:41:10 +0200 Subject: [PATCH] =?UTF-8?q?Docker=20learned=20to=20find=20unused=20images?= =?UTF-8?q?=20itself=20=E2=9D=A4=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Knut Ahlers --- docker-clean | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/docker-clean b/docker-clean index 3870522..d6be3e4 100755 --- a/docker-clean +++ b/docker-clean @@ -11,20 +11,5 @@ step "Removing containers created / exited >= ~1h ago..." CONTAINERS=$(docker ps -a | gawk '/(hours?|days?|weeks?|months?) ago\s+(Created|Exited)/{ print $1 }' | xargs) [ -n "${CONTAINERS}" ] && docker rm ${CONTAINERS} -step "Untagging all images not used by containers..." -PS_OUTPUT=$(docker ps -a) -for img in $(docker images | gawk '/^[a-z].*(hours?|days?|weeks?|months?) ago/{ print $1":"$2 }' | grep -v '' | sort | uniq); do - imghash=$(docker inspect "${img}" | jq -r '.[0].Id' | sed -E 's/^sha256:(.{12}).*/\1/') - - # Check if image is currently active - (echo "${PS_OUTPUT}" | grep -q "\s${img}\s") && continue - # Check if image with stripped ":latest" is currently active - (echo "${PS_OUTPUT}" | grep -q "\s${img%:latest}\s") && continue - # Check if image hash is currently active (sometimes docker looses named references) - (echo "${PS_OUTPUT}" | grep -q "\s${imghash}\s") && continue - - docker rmi --no-prune "${img}" -done - -step "Removing dangling images..." -docker image prune -f +step "Removing unused and dangling images..." +docker image prune -a -f