mirror of
https://github.com/luzifer-docker/docker-clean.git
synced 2024-12-20 08:21:15 +00:00
Docker learned to find unused images itself ❤️
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
6924335526
commit
885a8c553d
1 changed files with 2 additions and 17 deletions
19
docker-clean
19
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 '<none>' | 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
|
||||
|
|
Loading…
Reference in a new issue