Skip to content

Instantly share code, notes, and snippets.

@zeg-io
Created May 18, 2017 01:03
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save zeg-io/a016195c57332723fabf7528a9f2fb75 to your computer and use it in GitHub Desktop.
Save zeg-io/a016195c57332723fabf7528a9f2fb75 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"
@nikitamendelbaum
Copy link

description says 4 weeks, script does 4 hours

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment