Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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
@michaelneale

This comment has been minimized.

Show comment
Hide comment
@michaelneale

michaelneale Jul 28, 2014

Put this in your /bin somewhere and save some disk space!
Or buy a bigger disk and stop complaining.

Owner

michaelneale commented Jul 28, 2014

Put this in your /bin somewhere and save some disk space!
Or buy a bigger disk and stop complaining.

@DamonOehlman

This comment has been minimized.

Show comment
Hide comment
@DamonOehlman

DamonOehlman Jul 28, 2014

Thanks mate :)

Though a bigger disk does sound nice...

DamonOehlman commented Jul 28, 2014

Thanks mate :)

Though a bigger disk does sound nice...

@michaelneale

This comment has been minimized.

Show comment
Hide comment
@michaelneale

michaelneale Jul 28, 2014

also docker run --rm to stop them building up...

Owner

michaelneale commented Jul 28, 2014

also docker run --rm to stop them building up...

@twillouer

This comment has been minimized.

Show comment
Hide comment
@twillouer

twillouer Jul 28, 2014

docker rmi $(docker images -f dangling=true -q), no ? :)

twillouer commented Jul 28, 2014

docker rmi $(docker images -f dangling=true -q), no ? :)

@michaelneale

This comment has been minimized.

Show comment
Hide comment
@michaelneale

michaelneale Jul 28, 2014

@twillouer ah yes - for some reason that didn't make it from my local copy - well spotted. Corrected.

Owner

michaelneale commented Jul 28, 2014

@twillouer ah yes - for some reason that didn't make it from my local copy - well spotted. Corrected.

@michaelneale

This comment has been minimized.

Show comment
Hide comment
@michaelneale

michaelneale Jul 28, 2014

thanks to @huntc for mentioning "dangling" to be (I would have though it was a joke).

Owner

michaelneale commented Jul 28, 2014

thanks to @huntc for mentioning "dangling" to be (I would have though it was a joke).

@tfennelly

This comment has been minimized.

Show comment
Hide comment
@tfennelly

tfennelly Apr 23, 2015

I ran into this issue doing what seems to be fairly lightweight stuff. In my case, even after cleaning out all containers and images, I still have about 25Gb of images according to docker (docker images -a | tr -s ' ' | cut -d ' ' -f 7 | awk '{s+=$1} END {print s}'). That must not be totally accurate though because VB says that the VMDK image is 19Gb.

Going to try https://docs.docker.com/articles/b2d_volume_resize/ since apparently those VMDK images are not resizable.

tfennelly commented Apr 23, 2015

I ran into this issue doing what seems to be fairly lightweight stuff. In my case, even after cleaning out all containers and images, I still have about 25Gb of images according to docker (docker images -a | tr -s ' ' | cut -d ' ' -f 7 | awk '{s+=$1} END {print s}'). That must not be totally accurate though because VB says that the VMDK image is 19Gb.

Going to try https://docs.docker.com/articles/b2d_volume_resize/ since apparently those VMDK images are not resizable.

@ryanramage

This comment has been minimized.

Show comment
Hide comment
@ryanramage

ryanramage Oct 13, 2015

Should

docker rmi $(docker images -f dangling=true -q)

be

docker rmi -f $(docker images dangling=true -q)

Seems to me the -f is to force the delete of the image id.

ryanramage commented Oct 13, 2015

Should

docker rmi $(docker images -f dangling=true -q)

be

docker rmi -f $(docker images dangling=true -q)

Seems to me the -f is to force the delete of the image id.

@tupy

This comment has been minimized.

Show comment
Hide comment
@tupy

tupy May 19, 2016

Also remove volumes with docker volume rm $(docker volume ls -qf dangling=true)

tupy commented May 19, 2016

Also remove volumes with docker volume rm $(docker volume ls -qf dangling=true)

@kenyee

This comment has been minimized.

Show comment
Hide comment
@kenyee

kenyee 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 ;-)

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