Simplify cleanup

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2018-07-08 21:47:27 +02:00
parent 149dc46185
commit 106e6b74d7
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

View file

@ -8,16 +8,8 @@ else
fi fi
step "Removing containers created / exited >= ~1h ago..." step "Removing containers created / exited >= ~1h ago..."
CONTAINERS=$(docker ps -a | awk '/(hours?|days?|weeks?|months?) ago\s+(Created|Exited)/{ print $1 }' | xargs) CONTAINERS=$(docker ps -a | gawk '/(hours?|days?|weeks?|months?) ago\s+(Created|Exited)/{ print $1 }' | xargs)
[ -n "${CONTAINERS}" ] && docker rm ${CONTAINERS} [ -n "${CONTAINERS}" ] && docker rm ${CONTAINERS}
step "Untagging all images not used by containers..." step "Removing unused and dangling images..."
PS_OUTPUT=$(docker ps -a) docker image prune -a -f
for img in $(docker images | awk '/^[a-z].*(hours?|days?|weeks?|months?) ago/{ print $1":"$2 }' | grep -v '<none>' | sort | uniq); do
if ! (echo "${PS_OUTPUT}" | grep -q "\s${img}\s") && ! (echo "${PS_OUTPUT}" | grep -q "\s${img%:latest}\s"); then
docker rmi --no-prune "${img}"
fi
done
step "Removing dangling images..."
docker image prune -f