Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
docker cleanup guide: containers, images, volumes, networks

Docker - How to cleanup (unused) resources

Once in a while, you may need to cleanup resources (containers, volumes, images, networks) ...

delete volumes

// see:

$ docker volume rm $(docker volume ls -qf dangling=true)
$ docker volume ls -qf dangling=true | xargs -r docker volume rm

delete networks

$ docker network ls  
$ docker network ls | grep "bridge"   
$ docker network rm $(docker network ls | grep "bridge" | awk '/ / { print $1 }')

remove docker images

// see:

$ docker images
$ docker rmi $(docker images --filter "dangling=true" -q --no-trunc)

$ docker images | grep "none"
$ docker rmi $(docker images | grep "none" | awk '/ / { print $3 }')

remove docker containers

// see:

$ docker ps
$ docker ps -a
$ docker rm $(docker ps -qa --no-trunc --filter "status=exited")

Resize disk space for docker vm

$ docker-machine create --driver virtualbox --virtualbox-disk-size "40000" default

regarding bridge networks I find the following modification more robust:

docker network ls | awk '$3 == "bridge" && $2 != "bridge" { print $1 }'

In this example I do not want to delete the network named ($2) "bridge", if I am not mistaken that network is predefined by docker.

blankdots commented Apr 7, 2017

What about pruning the networks
$ docker network prune
And docker system prune
$ docker system prune

They seem less distructive.

@blankdots those are great, but are only available as of 1.13.

To delete volumes, use: docker volume rm $(docker volume ls -f 'dangling=true')
The "-f" parameter is necessary for describe the filter ('dangling=true'). [[]]'s

thebaconguy commented Oct 6, 2017

$ docker system prune -a

WARNING! This will remove:
	- all stopped containers
	- all volumes not used by at least one container
	- all networks not used by at least one container
	- all images without at least one container associated to them
Are you sure you want to continue? [y/N] y

----> from documentation example

I was looking for a clean everything (almost, see EDIT below) not running or in use command to do a clean reset before installing Rancher to manage the entire system (highly recommend watching the demo and trying it out btw!). Hope this comes in handy for someone else looking for a fresh start! You can still use the filtering arguments to prune only resources matching a certain label or before a certain timestamp.

Thanks to @blankdots for the suggestion above to look into the docker system prune commands!

EDIT: This does not seem to include removal of unused/dangling volumes, so @professordennis's suggestion is still best to use for that case:

$ docker volume rm $(docker volume ls -f dangling=true -q)

----> also found from this documentation

liweys commented Nov 2, 2017

how to clean it before starting docker? docker cannot be started due to no space.

just faced an inode exhaustion issue on ext4 & freed up ~18.5 million inodes out of 19 million total inodes using the image cleanup command.
Thank you!

tindvik commented Jan 4, 2018

@thebaconguy If you want to remove volumes with the prune command you can run this command:
docker system prune -a --volumes

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