Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vitorcalvi/80c6b97e7658ae38c8b9da364b90c436 to your computer and use it in GitHub Desktop.
Save vitorcalvi/80c6b97e7658ae38c8b9da364b90c436 to your computer and use it in GitHub Desktop.
docker cleanup guide: containers, images, volumes, networks

Docker - How to cleanup (unused) resources

One line (generated by ChatGPT 3.5)

sudo docker volume ls -qf dangling=true | xargs -r sudo docker volume rm && \
sudo docker network rm $(sudo docker network ls --format "{{.ID}} {{.Name}}" | grep -v "bridge" | awk '/ / { print $1 }') && \
sudo docker images --filter "dangling=true" -q --no-trunc | xargs -r sudo docker rmi && \
sudo docker images | grep "none" | awk '/ / { print $3 }' | xargs -r sudo docker rmi && \
sudo docker rm $(sudo docker ps -qa --no-trunc --filter "status=exited")

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

delete volumes

// see: https://github.com/chadoe/docker-cleanup-volumes

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

delete networks

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

remove docker images

// see: http://stackoverflow.com/questions/32723111/how-to-remove-old-and-unused-docker-images

$ sudo docker images
$ sudo docker images --filter "dangling=true" -q --no-trunc | xargs -r sudo docker rmi

$ sudo docker images | grep "none"
$ sudo docker images | grep "none" | awk '/ / { print $3 }' | xargs -r sudo docker rmi

remove docker containers

// see: http://stackoverflow.com/questions/32723111/how-to-remove-old-and-unused-docker-images

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

Resize disk space for docker vm

$ sudo docker-machine create --driver virtualbox --virtualbox-disk-size "40000" default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment