Skip to content

Instantly share code, notes, and snippets.

@michaelneale
Last active July 20, 2021 15:37
Show Gist options
  • Star 50 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save michaelneale/1366325a7737c4cb80b0 to your computer and use it in GitHub Desktop.
Save michaelneale/1366325a7737c4cb80b0 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)
}
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
;;
*)
echo "
usage: docker-clean containers|images
containers - removes all stopped containers it can.
images - removes dangling (un-needed) image layers - images you no longer need
"
;;
esac
@kenyee
Copy link

kenyee commented Dec 29, 2016

Be sure to do what tupy suggested only after you get your current containers running so you don't pure volumes that they might be using if they weren't running ;-)

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