cfg/bin/docker-clean

21 lines
803 B
Text
Raw Normal View History

2016-07-21 13:48:49 +00:00
#!/bin/bash
set -euo pipefail
2016-07-21 13:48:49 +00:00
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 | awk '/(hours?|days?|weeks?|months?) ago\s+(Created|Exited)/{ print $1 }' | xargs)
[ -n "${CONTAINERS}" ] && docker rm ${CONTAINERS}
step "Removing images missing repository or tag..."
IMGS=$(docker images | awk '/<none>.*(hours?|days?|weeks?|months?) ago/{ print $3 }' | xargs)
[ -n "${IMGS}" ] && docker rmi ${IMGS}
step "Removing images with tags not being used (used images will throw errors, that's normal)..."
IMGS=$(docker images | awk '/(hours?|days?|weeks?|months?) ago/{ print $1":"$2 }' | xargs)
[ -n "${IMGS}" ] && docker rmi ${IMGS} || true