Introduction
Docker makes it easy to package your applications and services into containers so you can run them anywhere. However, as you work with Docker, it's also easy to accumulate a large number of unused images, containers, and data volumes that clutter up output and eat up 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 up disk space and organizing your system by removing unused Docker images, containers, and volumes.
How to use this guide:
- This guide is in the form of a cheat sheet with stand-alone command line snippets.
- Go to each section related to the task you want to complete.
Delete all images, containers, and volumes
Docker provides a single command that cleans up any resource such as images, containers, volumes, and networks:
docker system pruneTo remove stopped containers and all unused images, add -a to the command:
docker system prune -aDelete Docker images
Use the docker images command with -a to find the ID of the images you want to remove. This will show you each image, including the middle layers of the image. Once you have found the images you want to remove, you can pass their ID or tag to docker rmi:
List:
docker images -aRemoval:
docker rmi Image ImageRemove hanging images
Docker images are made up of multiple layers. Dangling images are layers that have no connection to any tagged images. They no longer serve a purpose and are consuming disk space. They can be found by adding the -f filter with the value dangling=true to the docker images command. When you are sure you want to remove them, you can use the docker image prune command:
List:
docker images -f dangling=trueRemoval:
docker image pruneDelete images based on a pattern
Using a combination of docker images and grep you can find all images that match a pattern. Once you are satisfied, you can remove them using awk to pass the IDs to docker rmi. Note that these tools are not provided by Docker and are not necessarily available on all systems:
List:
docker images -a | grep "pattern"Removal:
docker images -a | grep "pattern" | awk '{print $3}' | xargs docker rmiDelete all images.
All Docker images on a system can be added by adding -a List all the images with the docker images command. Once you are sure you want to remove them all, you can -q Add to pass the image ID to docker rmi Send:
List:
docker images -aRemoval:
docker rmi $(docker images -a -q)Remove (remove template):
Use the docker ps command with -a to find the name or ID of the containers you want to remove:
List:
docker ps -aRemoval:
docker rm ID_or_Name ID_or_NameRemove all output format
You can use containers with docker ps -a Find and filter them by their status: created, restarted, running, paused, or exited. To review the list of exited containers, use -f to filter by status. Once you've confirmed that you want to remove those containers, use -q to pass the identifiers to the docker rm command:
List:
docker ps -a -f status=exitedRemoval:
docker rm $(docker ps -a -f status=exited -q)Remove template using more than one filter
Docker filters can be combined by repeating the filter with an additional value. This results in a list of containers that meet each of these conditions. For example, if you want to remove all containers that are marked as created (a state that occurs when a container is run with an invalid command) or exited, you can use two filters:
List:
docker ps -a -f status=exited -f status=createdRemoval:
docker rm $(docker ps -a -f status=exited -f status=created -q)Cut the mold according to a pattern.
You can find all containers that match a pattern using a combination of docker ps and grep . Once you are satisfied with the list you want to remove, you can use awk and xargs to provide the ID to docker rm . Note that these tools are not provided by Docker and are not necessarily available on all systems:
List:
docker ps -a | grep "pattern”Removal:
docker ps -a | grep "pattern" | awk '{print $1}' | xargs docker rmStop and remove all templates.
You can check the containers on your system with docker ps . Adding -a will show all containers. Once you are sure you want to remove them, you can add -q to the docker stop and docker rm commands to provide the identifiers:
List:
docker ps -aRemoval:
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)Delete volumes
Delete one or more specific volumes – Docker 1.9 and above
Since volumes are intended to exist independently of containers, when a template is removed, a volume is not automatically deleted at the same time. When a volume exists and is no longer attached to any container, it is called a dangling volume. To determine their location and confirm that you want to delete them, you can use the docker volume ls command with a filter to limit the results to dangling volumes. Once you are satisfied with the list, you can delete them all with docker volume prune:
List:
docker volume ls -f dangling=trueRemoval:
docker volume pruneTake a template and its volume.
If you have created an unnamed volume, it can be removed at the same time as the container with -v. Note that this only works with unnamed volumes. When the volume is successfully removed, its ID is displayed. Note that there is no mention of the volume being removed. If it is unnamed, it is silently removed from the system. If it has a name, it is silently present.
Removal:
docker rm -v container_nameResult
This guide covers some of the common commands used to delete images, containers, and volumes with Docker. There are many other combinations and flags that can be used with each of them. For a comprehensive guide to what is available, see the Docker documentation for system pruning. docker, docker rmi, docker-rm and docker volume rm See. If there are any common cleaning tasks you would like to see in the guide, please ask or make suggestions in the comments.









