Skip to content

Instantly share code, notes, and snippets.

@gmas
Created May 2, 2015 03:54
Show Gist options
  • Save gmas/e3059b33f68bd176cc96 to your computer and use it in GitHub Desktop.
Save gmas/e3059b33f68bd176cc96 to your computer and use it in GitHub Desktop.
cleanup unused docker containers and images
#!/usr/bin/env bash
#set IFS to split lines into words
IFS=$'\n'
#find old stopped containers
OLD_CONTAINERS=( $(docker ps -a| grep -v latest | grep -v 'IMAGE' | awk '{print $1}') )
#find images older than 1 day
OLD_IMGS=( $(docker images | grep -v latest | grep 'days\|week\|month\|year' | awk '{print $3}') )
unset IFS
echo "found ${#OLD_CONTAINERS[@]} old/unused containers"
for CONTAINER in "${OLD_CONTAINERS[@]}" ; do
echo "deleting old container: $CONTAINER"
docker rm $CONTAINER
done
echo "found ${#OLD_IMGS[@]} images older than 1 day"
for IMG in "${OLD_IMGS[@]}" ; do
echo "deleting image: $IMG"
docker rmi $IMG
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment