Original: https://docs.docker.com/engine/admin/pruning/ abstract
For objects that are no longer used, such as mirrors, containers, volumes, and network Docker, are passive cleanup (analogy garbage collection) mechanisms: they are generally not erased unless manually cleaned with the commands provided by Docker. These unused objects will of course occupy additional valuable space resources. Docker provides individual object Cleanup command prune for different objects, and Docker system prune commands to clean up multiple unused objects at once. What we're going to talk about today is these commands for object cleanup: prune. Prune Images command docker image prune to clean up Docker images that are no longer in use. Performing Docker image prune clears the "dangling" mirror by default. What is a "dangling" image. An image that has neither a signature nor a container reference is called an "dangling" image. Here's how:
$ docker Image Prune
warning! This would remove all dangling images.
Is you sure want to continue? [y/n] Y
Sometimes we want to clear all images without a container reference, add a-a flag to get it done:
$ docker Image Prune-a
warning! This would remove all images without at least one container associated to them.
Is you sure want to continue? [y/n] Y
The clear action will remind you if you really want to clear the object, by default the option will be yes, but if you are too prompt, you can use the-F or--force flag to force the cleanup.
More humane, Docker provides a--filter flag to filter out the images that you want to keep. For example, you can do this only by clearing a mirror that exceeds the creation time of more than 24 hours:
$ docker Image prune-a--filter "until=24h"
Of course, I can also customize my image cleanup plan with other expressions. More examples refer to Docker image prune.
Prune containersIf the--RM option is not specified when the container starts, it cannot be automatically cleared when the container is stopped. Sometimes we do nothing to knock down the Docker ps-a command will be surprised to find that, God, there are so many containers, there are running there are also stopped. Where did they come from? Whether or not they are concerned. This situation is particularly common in a development environment. You know, even if the container has been stopped, it will take up space resources. Well, let's end this mess with the Docker container prune command:
$ docker Container Prune
warning! This would remove all stopped containers.
As in the case of image cleanup, there will be a message telling you whether to continue, the default is yes, and if the message bothers you, add-f or--force flag to force clear.
By default, the Docker container prune command cleans up all containers that are in stopped state, and if you don't want to be cruel, you can also use the--filter flag to filter out containers that you don't want to be cleaned out. Here is an example of a filter that clears all the stopped containers except those created within 24:
$ docker Container prune--filter "until=24h"
Other filters can be implemented by: Docker container prune reference, here are more detailed examples.
Prune volumesVolumes can be used by one or more containers consumes the host side of the space, but it does not automatically clean up, because it is possible to destroy the useful data.
$ docker Volume Prune
warning! This would remove all volumes not used by at least one container.
Is you sure want to continue? [y/n] Y
As with Conatiner, the manual cleanup volume will have a prompt message, adding the-F or--force flag can skip the prompt to clean up the message directly. Use the filter parameter--filter to filter out useless volume that you do not want to clean up, otherwise all unused volumes will be cleaned up by default. The following example shows a cleanup (volume without references) outside of Lable=keep except for volume:
$ Docker Volume prune--filter "Label!=keep"
Other filtering criteria can be implemented as a reference: Docker volume prune reference, which gives more examples of references.
Prune NetworksWhile Docker networks takes up a lot of space, it creates iptable rules, virtual bridge devices, and routing table entries, and you'll freak out when you see so many zombie objects. Of course, we still need to clean up the artifact: The Docker network prune to clean up the networks that is no longer referenced by any container:
$ docker Network Prune
warning! This would remove all networks not used by at least one container.
Is you sure want to continue? [y/n] Y
You can use the-F or--force flag to skip the prompt to force the command to execute. By default, all networks that are not referenced are cleared and can be implemented using--filter if you want to filter some specific networks. The following example uses--filter to clean up networks that have not been referenced and created for more than 24 hours:
$ docker Network prune--filter "until=24h"
For more information on the--filter of the Docker network, refer to the example: Docker network prune reference.
Prune everythingTitle, here is to clean everything:images, containers,networks One-time cleanup operation can be done through the Docker system prune. In Docker 17.06.0 and earlier versions, this Docker system prune will also clean up volume together; In 17.06.1 and later versions, you must manually specify the--VOLUMES flag to be able to clean out the volumes:
$ docker System Prune
warning! This would remove:
-All stopped containers
-all networks not used by @ least one container
-all dangling IM Ages
-All build cache is
sure you want to continue? [y/n] Y
When you add a--volumes flag to a Docker 17.06.1 or later version:
$ docker System Prune--volumes
warning! This would remove:
-All stopped containers
-all networks not used by @ least one container
-all volumes not Used by at least one container
-all dangling images-all build cache is you sure do want to
continue? [y/n] Y