mirror of
https://github.com/luzifer-docker/docker-clean.git
synced 2024-11-14 08:42:49 +00:00
Check whether image is in use by hash
this seems sometimes to be required as the Docker daemon looses the reference and does not display the name in `docker ps -a` output... Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
e5bf6c53af
commit
6924335526
2 changed files with 14 additions and 6 deletions
|
@ -4,7 +4,8 @@ RUN set -ex \
|
||||||
&& apk --no-cache add \
|
&& apk --no-cache add \
|
||||||
bash \
|
bash \
|
||||||
docker \
|
docker \
|
||||||
gawk
|
gawk \
|
||||||
|
jq
|
||||||
|
|
||||||
COPY docker-clean /usr/local/bin/
|
COPY docker-clean /usr/local/bin/
|
||||||
|
|
||||||
|
|
11
docker-clean
11
docker-clean
|
@ -14,9 +14,16 @@ CONTAINERS=$(docker ps -a | gawk '/(hours?|days?|weeks?|months?) ago\s+(Created|
|
||||||
step "Untagging all images not used by containers..."
|
step "Untagging all images not used by containers..."
|
||||||
PS_OUTPUT=$(docker ps -a)
|
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
|
for img in $(docker images | gawk '/^[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
|
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}"
|
docker rmi --no-prune "${img}"
|
||||||
fi
|
|
||||||
done
|
done
|
||||||
|
|
||||||
step "Removing dangling images..."
|
step "Removing dangling images..."
|
||||||
|
|
Loading…
Reference in a new issue