mirror of
https://github.com/luzifer-docker/docker-clean.git
synced 2024-11-09 22:40:07 +00:00
Initial version
This commit is contained in:
commit
31d54f70ef
2 changed files with 34 additions and 0 deletions
11
Dockerfile
Normal file
11
Dockerfile
Normal 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
23
docker-clean
Executable 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
|
Loading…
Reference in a new issue