Initial version

This commit is contained in:
Knut Ahlers 2018-07-07 11:48:19 +02:00
commit 31d54f70ef
Signed by: luzifer
GPG Key ID: DC2729FDD34BE99E
2 changed files with 34 additions and 0 deletions

11
Dockerfile Normal file
View File

@ -0,0 +1,11 @@
FROM alpine:3.8
RUN set -ex \
&& apk --no-cache add \
bash \
docker \
gawk
COPY docker-clean /usr/local/bin/
ENTRYPOINT ["docker-clean"]

23
docker-clean Executable file
View File

@ -0,0 +1,23 @@
#!/bin/bash
set -euo pipefail
if [ -e "${HOME}/bin/script_framework.sh" ]; then
source "${HOME}/bin/script_framework.sh"
else
function step() { echo $@; }
fi
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
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