How To Remove Docker Images, Containers, and Volumes
Introduction
Docker makes it easy to wrap your applications and services in containers so you can run them anywhere. However, as you work with Docker, it’s also easy to accumulate an excessive number of unused images, containers, and data volumes that clutter the output and consume disk space.
Docker gives you all the tools you need to clean up your system from the command line. This cheat sheet-style guide provides a quick reference to commands that are useful for freeing disk space and keeping your system organized by removing unused Docker images, containers, and volumes.
How to Use This Guide
- This guide is in cheat sheet format with self-contained command-line snippets.
- Jump to any section that is relevant to the task you are trying to complete.
- Note: The command substitution syntax,
$(command)
, is available in many popular shells such as bash, zsh, and Windows Powershell.
Purging All Unused or Dangling Images, Containers, Volumes, and Networks
Docker provides a single command that will clean up any resources — images, containers, volumes, and networks — that are dangling (not tagged or associated with a container):
docker system prune
To additionally remove any stopped containers and all unused images (not just dangling images), add the -a
flag:
docker system prune -a
Removing Docker Images
Remove one or more specific images
Use the docker images
command with the -a
flag to locate the ID of the images you want to remove. Then use docker rmi
to remove them:
docker images -a
docker rmi
Remove dangling Docker images
To locate dangling images:
docker images -f dangling=true
To remove them:
docker image prune
Removing Containers
Remove one or more specific containers
Use docker ps -a
to locate the containers you want to remove:
docker ps -a
docker rm
Remove all exited containers
To locate exited containers:
docker ps -a -f status=exited
To remove them:
docker rm $(docker ps -a -f status=exited -q)
Removing Volumes
Remove one or more specific volumes
Use the docker volume ls
command to locate volumes:
docker volume ls
Then use docker volume rm
to remove them:
docker volume rm
Remove dangling volumes
To locate dangling volumes:
docker volume ls -f dangling=true
To remove them:
docker volume prune
Conclusion to Remove Docker Images
This guide covers some of the common commands used to remove images, containers, and volumes with Docker.