Skip to content

Instantly share code, notes, and snippets.

@helielson
Forked from michaelneale/docker-clean
Last active May 18, 2016 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helielson/423587713cf5420a1dac64b626627ed4 to your computer and use it in GitHub Desktop.
Save helielson/423587713cf5420a1dac64b626627ed4 to your computer and use it in GitHub Desktop.
#!/bin/sh
remove_dangling() {
echo "Removing dangling images ..."
docker rmi $(docker images -f dangling=true -q)
}
remove_stopped_containers() {
echo "Removing stopped containers ..."
docker rm $(docker ps -qa)
}
remove_volumes() {
echo "Removing dangling volumes ..."
docker volume rm $(docker volume ls -qf dangling=true)
}
case $1 in
images)
remove_dangling
;;
containers)
read -p "Are you sure you want to remove all stopped containers?" -n 1 -r
echo #
if [[ $REPLY =~ ^[Yy]$ ]]
then
remove_stopped_containers
fi
;;
volumes)
read -p "Are you sure you want to remove all dangling volumes?" -n 1 -r
echo #
if [[ $REPLY =~ ^[Yy]$ ]]
then
remove_volumes
fi
;;
*)
echo "
usage: docker-clean containers|images|volumes
containers - removes all stopped containers it can.
images - removes dangling (un-needed) image layers - images you no longer need
volumes - removes dangling (un-needed) volumes - volumes you no longer need
"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment