introduce
Docker can easily encapsulate applications and services in containers to run anywhere. However, it is easy to accumulate a large number of unused images, containers, and data volumes during use of Docker, which can clutter the output and take up disk space.
Docker provides all the necessary tools to clean up the system via the command line. This quick lookup table style guide provides a quick reference listing of useful commands to free up disk space and keep the system organized by deleting unused Docker images, containers, and volumes.
How to use this guide:
- This guide is in a quick lookup table format and contains independent command line snippets.
- You can jump directly to any part related to the task you want to complete.
Notice
If you want to deploy Docker applications in one click to a live server, check out the DigitalOcean App Platform.
Clean up all unused or hanging images, containers, volumes, and networks
Docker provides a single command to clean up any unused resources—including mirrors, containers, volumes, and networks—which are "dangling" (no tags or associated with containers):
docker system prune
To additionally remove any stopped containers and all unused images (not just dangling images), add in the command-a
Logo:
docker system prune -a
Delete Docker image
Delete one or more specific images
usedocker images
Commands and-a
Flags to locate the ID of the image to be deleted. This will display each image, including the intermediate image layer. When you find the images you want to delete, you can pass their IDs or tags todocker rmi
:
List:
docker images -a
delete:
docker rmi Image Image
Delete the hanging mirror
A Docker image consists of multiple layers. A dangling mirror is a layer that has no association with any marked mirror. They no longer work and take up disk space. They can be positioned by adding the filter flag -f to the docker images command with a value of dangling=true. When you are sure you want to delete them, you can use the docker image prune command:
List:
docker images -f dangling=true
delete:
docker image prune
Delete the mirror according to the mode
You can usedocker images
andgrep
A combination of to find all images that match a pattern. Once you are satisfied, you can useawk
Pass the ID todocker rmi
To delete them. Note that these utilities are not provided by Docker and are not necessarily available on all systems:
List:
docker images -a | grep "pattern"
delete:
docker images -a | grep "pattern" | awk '{print $3}' | xargs docker rmi
Delete all images
You can use it todocker images
Command Add-a
To list all Docker images on the system. Once you are sure you want to delete them, you can add-q
The flag passes the mirror ID todocker rmi
:
List:
docker images -a
delete:
docker rmi $(docker images -a -q)
Delete container
Delete one or more specific containers
usedocker ps
Commands and-a
Flags to locate the name or ID of the container to be deleted:
List:
docker ps -a
delete:
docker rm ID_or_Name ID_or_Name
Delete the container when exiting
If you know that when creating a container, you don't need to keep it once it's done, you can rundocker run --rm
Come and delete it automatically when exiting:
Run and delete:
docker run --rm image_name
Delete all exited containers
Can be useddocker ps -a
Position containers and filter according to their status:created
、restarting
、running
、paused
orexited
. To view the list of "exited" containers, use-f
Flags are filtered based on status. When you verify that you want to delete these containers, use-q
Pass the ID todocker rm
Order:
List:
docker ps -a -f status=exited
delete:
docker rm $(docker ps -a -f status=exited -q)
Use multiple filters to delete containers
Docker filters can be combined by reusing the filter flag and additional values. This will result in a list of containers that meet either condition. For example, if you want to delete all tags ascreated
(The state that may occur when you run the container with an invalid command) orexited
container, you can use two filters:
List:
docker ps -a -f status=exited -f status=created
delete:
docker rm $(docker ps -a -f status=exited -f status=created -q)
Delete containers according to pattern
You can usedocker ps
andgrep
A combination of to find all containers that match the pattern. When you determine the list you want to delete, you can useawk
andxargs
Provide ID todocker rm
. Note that these utilities are not provided by Docker and are not necessarily available on all systems:
List:
docker ps -a | grep "pattern”
delete:
docker ps -a | grep "pattern" | awk '{print $1}' | xargs docker rm
Stop and delete all containers
You can usedocker ps
To view the containers on the system. Add to-a
The flag will display all containers. When you are sure you want to delete them, you can add-q
The flag provides the ID todocker stop
anddocker rm
Order:
List:
docker ps -a
delete:
docker stop $(docker ps -a -q) docker rm $(docker ps -a -q)
Delete volume
Delete one or more specific volumes - Docker 1.9 and later
usedocker volume ls
The command locates the name of the volume to be deleted, and then you can usedocker volume rm
Command to delete one or more volumes:
List:
docker volume ls
delete:
docker volume rm volume_name volume_name
Delete dangling volumes - Docker 1.9 and later
Since the existence of a volume is container-independent, the volume will not be automatically deleted at the same time when the container is deleted. When a volume exists and is no longer connected to any container, it is called a dangling volume. To confirm that you want to delete them, you can usedocker volume ls
Command and use filters to limit the results to dangling volumes. When you are satisfied with the list, you can usedocker volume prune
Delete them:
List:
docker volume ls -f dangling=true
delete:
docker volume prune
Delete containers and volumes
If you create an unnamed volume, it can be deleted at the same time when deleting the container, use-v
Lottery. Please note that this only applies to unnamed volumes. When the container is successfully deleted, its ID will be displayed. Please note that no mention of deleting volumes is mentioned. If it is unnamed, it will be silently removed from the system. If it has a name, it will remain silently.
delete:
docker rm -v container_name
in conclusion
This guide covers some of the common commands for using Docker to delete images, containers, and volumes. There are many other combinations and flags available for each command. For a comprehensive guide to what is available, see docker system prune, docker rmi, docker rm, and docker volume rm in the Docker documentation. If you would like to see common cleaning tasks in your guide, please make or make suggestions in the comments.
The above is the detailed content of the tutorial guide for deleting Docker images, containers and volumes. For more information about deleting Docker images, containers and volumes, please follow my other related articles!