Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save JPvRiel/2a2b37861413f70cd140018b6f50f880 to your computer and use it in GitHub Desktop.
Save JPvRiel/2a2b37861413f70cd140018b6f50f880 to your computer and use it in GitHub Desktop.
How to remove unused docker containers and images (cleanup)

Delete all containers

$ docker ps -q -a | xargs docker rm
  • -q prints only the container IDs
  • -a prints all containers

Notice that it uses xargs to issue a remove container command for each container ID

Delete images

Delete all untagged images

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

awk must use a single quote (this filters all image IDs)

Delete all images

$ docker rmi $(docker images -q)

Delete all dangling volumes

for v in $(sudo docker volume ls -qf 'dangling=true'); do sudo docker volume rm "$v"; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment