mirror of
https://github.com/luzifer-docker/docker-clean.git
synced 2024-12-20 16:21:18 +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 \
|
||||
bash \
|
||||
docker \
|
||||
gawk
|
||||
gawk \
|
||||
jq
|
||||
|
||||
COPY docker-clean /usr/local/bin/
|
||||
|
||||
|
|
17
docker-clean
17
docker-clean
|
@ -2,9 +2,9 @@
|
|||
set -euo pipefail
|
||||
|
||||
if [ -e "${HOME}/bin/script_framework.sh" ]; then
|
||||
source "${HOME}/bin/script_framework.sh"
|
||||
source "${HOME}/bin/script_framework.sh"
|
||||
else
|
||||
function step() { echo $@; }
|
||||
function step() { echo $@; }
|
||||
fi
|
||||
|
||||
step "Removing containers created / exited >= ~1h ago..."
|
||||
|
@ -14,9 +14,16 @@ CONTAINERS=$(docker ps -a | gawk '/(hours?|days?|weeks?|months?) ago\s+(Created|
|
|||
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
|
||||
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
|
||||
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..."
|
||||
|
|
Loading…
Reference in a new issue