This command is used to delete unused data in the Docker system
1. Introduction
Official website description: Delete all unused containers, networks, images (including hanging and unreferenced), and volumes (optional).
Explanation of nouns:
- Unused containers: All stopped containers will be deleted.
- Unused mirror: Only hanging images (not referenced by any container) will be removed unless the -a or --all parameters are used.
- Unused network: All custom networks that are not used by containers will be deleted.
- Unused volume(If using the --volumes or -v option): All volumes that are not referenced by the container will be deleted.
Related parameters:
- -a, --all: Delete all images that are not referenced by any container, not just hanging images.
- –filter: Filter the content to be deleted according to the provided conditions.
- –force, -f: Skip the confirmation step and execute the deletion directly. If you do not add it, you need to manually confirm the steps when executing them. It is recommended not to use it. Higher manual error tolerance
- –volumes, -v: Delete all volumes that are not referenced by at least one container.
2. Use examples
Simple example:
- Delete all unused containers, images, and networks:docker system prune
- Delete all unused containers, images (including uncited), networks:docker system prune -a
- Delete all unused containers, images, networks, volumes:docker system prune --volumes
Filtering (–filter)
until filter
This filter allows deleting containers, images, and networks created before a given timestamp.
Supports Unix timestamps, date format timestamps, or Go duration strings (such as 10m, 1h30m), which are calculated relative to the time of the daemon machine.
docker system prune --filter "until=1h"
label filter
labelFilters allow filtering based on the labels of containers, mirrors, networks and volumes. There are two formats:label=orlabel== Only objects with the specified tag will be deleted, and label!=or ** label!==** will delete objects that do not have the specified label.
Suppose there is a "nginx" container with the version tag "1.21.0". Tag filters can be used as follows:
The first: Delete all unused containers, images and networks with the "version=1.21.0" tag
docker system prune --filter "label=version=1.21.0"
The second type: Delete all unused containers, images and networks without the "app=nginx" tag
docker system prune --filter "label!=app=nginx"
Can also be used with -a and -v
docker system prune -a -v --filter "label!=app=nginx"
where labels can be viewed using docker inspect <container id or name>
Below is the Labels section of a container
{ "Labels": { "-hash": "b637c41bf29efe8fcd1d3c7baa3ad5ba8dc44f21cec0d3937d665fb79df76644", "-number": "1", "": "False", "": "jpom", ".config_files": "", ".working_dir": "/www/docker-compose/jpom", "": "jpom", "": "1.29.2" } }
Here is an example:
1. Delete all containers, images and networks for a specific project:
docker system prune --filter "label==jpom"
2.Delete all containers, images, and networks for a specific service:
docker system prune --filter "label==jpom"
3. Remove all containers, images and networks created with a specific version of Docker Compose:
docker system prune --filter "label==1.29.2"
Although this command can release resources, it needs to be carefully confirmed during execution to avoid accidentally deleting data. If necessary, important data need to be backed up.
Note: docker system prune -a is a very useful Docker command that also requires careful use. The purpose of this command is to clean up resources that are no longer needed in the Docker environment to save storage space. These resources include stopped containers, unused networks, hanging images, and build caches.
Specifically, docker system prune -a will do the following:
- Delete all stopped containers: This can help free up storage space occupied by these containers.
- Delete all unused networks: These networks should usually be deleted when they are no longer needed.
- Delete all hanging images: Hanging images are mirrors without labels. These images are usually hung because the new version of the mirror is pulled, and the old version of the mirror loses its original label.
- Thanks to the -a option, the command also deletes all images that are not referenced by any container, not just hanging images. This means that even if a certain image has a tag, if it is not used by any container, it will be deleted.
- Delete all build caches: These caches are usually used to speed up the build of Docker images, but in some cases they can take up a lot of storage space.
Be very careful when using this command, as once the resource is deleted, it cannot be restored. Before executing this command, it is recommended to back up all important data and containers.
If you want to clean up specific resources, such as just deleting a stopped container, you can use other more specific commands such as docker container prune
Summarize
This is the end of this article about the detailed explanation of the use example of the docker system prune command. For more related docker system prune command content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!