Skip to content

Instantly share code, notes, and snippets.

@johnpapa
Forked from spboyer/DockerCleanupScripts.md
Created August 3, 2017 01:37
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save johnpapa/0bd3df2a21b3a448b97ef3f429bd7145 to your computer and use it in GitHub Desktop.
Save johnpapa/0bd3df2a21b3a448b97ef3f429bd7145 to your computer and use it in GitHub Desktop.
Docker Cleanup Scripts

Docker - How to cleanup (unused) resources

Cleanup resources (containers, volumes, images, networks) ...

delete volumes

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

$ 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: http://stackoverflow.com/questions/32723111/how-to-remove-old-and-unused-docker-images

$ 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: http://stackoverflow.com/questions/32723111/how-to-remove-old-and-unused-docker-images

$ docker ps
$ docker ps -a
$ docker rm $(docker ps -qa --no-trunc --filter "status=exited")
@johnpapa
Copy link
Author

johnpapa commented Aug 4, 2017

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

@MattFriedman
Copy link

You could use xargs which would do nothing if no images of the type specified were found instead of showing an error.

For example:

docker ps -qa --no-trunc --filter "status=exited" | xargs docker rm

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