Skip to content

Instantly share code, notes, and snippets.

@khanhicetea
Created November 6, 2016 02:59
Show Gist options
  • Save khanhicetea/9e1972a12f0d9a1ef3e5531d3a7c5cb0 to your computer and use it in GitHub Desktop.
Save khanhicetea/9e1972a12f0d9a1ef3e5531d3a7c5cb0 to your computer and use it in GitHub Desktop.
Docker Garbage Collector
$ # Ref : https://github.com/docker/docker/pull/26108#issuecomment-243259942
$ # caveat that you get warnings on things that can't be removed
$ # remove all stopped containers
$ docker ps -aq | xargs --no-run-if-empty docker rm
$ # remove all unused volumes
$ docker volume ls -q | xargs --no-run-if-empty docker volume rm
$ # remove local volumes
$ docker volume ls | awk '/^local/ { print $2 }' | xargs --no-run-if-empty docker volume rm
$ # delete untagged images
$ docker images --filter dangling=true -q | xargs --no-run-if-empty docker rmi
$ # delete and untag every image that is not a container
$ # a little more heinous since "<none>" is the repo/tag for dangling images
$ docker images | awk 'NR>1 { if ($1 == "<none>") print $3; else print $1":"$2 }' | sort | xargs --no-run-if-empty docker rmi
$ # sort is not necessary but is nice if you add a -tn1 to xargs so you can see each rm line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment