2016-07-21 13:48:49 +00:00
|
|
|
#!/bin/bash
|
2018-01-13 18:05:15 +00:00
|
|
|
set -euo pipefail
|
2016-07-21 13:48:49 +00:00
|
|
|
|
2018-01-13 18:05:15 +00:00
|
|
|
if [ -e "${HOME}/bin/script_framework.sh" ]; then
|
|
|
|
source "${HOME}/bin/script_framework.sh"
|
|
|
|
else
|
2018-03-10 11:47:55 +00:00
|
|
|
function step() { echo $@; }
|
2018-01-13 18:05:15 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
step "Removing containers created / exited >= ~1h ago..."
|
|
|
|
CONTAINERS=$(docker ps -a | awk '/(hours?|days?|weeks?|months?) ago\s+(Created|Exited)/{ print $1 }' | xargs)
|
|
|
|
[ -n "${CONTAINERS}" ] && docker rm ${CONTAINERS}
|
|
|
|
|
2018-02-15 21:53:35 +00:00
|
|
|
step "Untagging all images not used by containers..."
|
|
|
|
PS_OUTPUT=$(docker ps -a)
|
|
|
|
for img in $(docker images | awk '/^[a-z].*(hours?|days?|weeks?|months?) ago/{ print $1":"$2 }' | grep -v '<none>' | sort | uniq); do
|
2018-03-10 11:47:55 +00:00
|
|
|
if ! (echo "${PS_OUTPUT}" | grep -q "\s${img}\s") && ! (echo "${PS_OUTPUT}" | grep -q "\s${img%:latest}\s"); then
|
2018-02-15 21:53:35 +00:00
|
|
|
docker rmi --no-prune "${img}"
|
|
|
|
fi
|
|
|
|
done
|
2018-01-13 18:05:15 +00:00
|
|
|
|
2018-02-15 21:53:35 +00:00
|
|
|
step "Removing dangling images..."
|
|
|
|
docker image prune -f
|