Skip to content

Instantly share code, notes, and snippets.

@stenio123
Forked from zeg-io/dk-clean.sh
Created October 23, 2017 19:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stenio123/4e7ebb421d72d9d2fdf1d4c1db1882e5 to your computer and use it in GitHub Desktop.
Save stenio123/4e7ebb421d72d9d2fdf1d4c1db1882e5 to your computer and use it in GitHub Desktop.
Clean all Docker images older than 4 weeks
oldContainers="$(docker ps -f "status=exited" | grep -E 'Exited \(.*\) [5-9] h|Exited \(.*\) \d\d h' | awk '{ print $1 }')"
echo -e -n "\nRemoving containers older than 4 hours"
if [ "$oldContainers" != "" ]; then
echo ""
docker rm $oldContainers
else
echo "...none found."
fi
the_nones="$(docker images | grep "<none>" | awk '{ print $3, $4, $5 }')"
old="$(echo -e "$the_nones" | grep "months\|days")"
# echo -e "$the_nones" | grep -e "[2-9]\shour[s]\|week[s]"
greaterThanFourHours="$(echo -e "$the_nones" | grep -e "[5-9]\shour[s]\|\d\d\shours\|day[s]\|week[s]\|month[s]\|year[s]")"
removeList="$(echo -e "$old" | awk '{ print $1 }')"
removeList=$removeList"$(echo -e "$greaterThanFourHours" | awk '{ print $1 }')"
echo -e -n "Removing images older than 4 hours"
if [ "$removeList" != "" ]; then
echo ""
docker rmi $removeList
else
echo "...none found."
fi
echo -e "Docker has been sanitized.\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment