How to quickly clean up Docker useless resources

Source: Internet
Author: User

If you use Docker frequently, you will find that the resources used by Docker are inflated quickly, and the most obvious and most noticeable of these should be disk space usage. This article describes how to quickly clean up the system resources that Docker consumes, specifically by removing useless mirrors, containers, networks, and data volumes.

View Docker-occupied resources
We need to figure out which system resources Docker is using before we can clean up resources. This needs to be done using a combination of different commands.

Docker container ls: The default is to list only the containers that are running, and the-a option lists all containers that include stop.
Docker image ls: Lists the image information, the-a option lists the intermediate image (which is the layer that other mirrors depend on).
Docker Volume LS: Lists the data volumes.
Docker Network LS: Lists the network.
Docker Info: Displays system-level information, such as the number of containers and mirrors.

After viewing the resources used by Docker with these commands, I'm sure you've decided to clean up some of the resources that Docker uses! Let's start with resources that are not being used.

Delete only those resources that are not in use
Docker provides a convenient Docker system prune command to remove stopped containers, dangling mirrors, network that is not referenced by the container, and caches in the build process:

$ Docker System Prune

For security reasons, this command does not delete data volumes that are not referenced by any container by default, and you need to explicitly specify the--volumns parameter if you need to delete the data volumes at the same time. For example, you might want to execute the following command:

$ docker System Prune--all--force--volumns
Not only will the data volume be deleted this time, but even the process of confirmation is gone! Note that when you use the--all parameter, all unreferenced mirrors are removed, not just the dangling mirror.

Here it is necessary to explain what is dangling images, in fact, can be understood as a simple image is not referenced by any image. For example, after you re-build the image, the mirrored layers that were previously built and no longer referenced become dangling images:

After the local image is updated, a <none> image in the red box appears similar to the image. This means that the old mirrors are no longer referenced, and they become dangling images. If you use the-a parameter, you will also find another type of <none> mirror, their repository and tag columns are shown as <NONE>

These mirrors are referred to as intermediate mirrors (the layers that other mirrors depend on).

We can also execute prune in different sub-commands, so that a class of resources is deleted:

Docker Container Prune # Remove all containers from exit status
Docker Volume Prune # Delete unused data volumes
Docker Image Prune # Delete dangling or all unused mirrors

Let Docker go back to the state of the installation
The "state at installation" here refers to the resource usage rather than the Docker configuration. This is also a common use case, for example, I need to automate the restoration of a production environment (using backup data from a production environment) for a bug investigation in a clean Docker environment. Let's see what we need to do together.

Recall that the Docker system prune--all--force--volumns command We described earlier, if all the containers in the system have been stopped before executing this command, then this command will remove all resources! OK, now let's try to stop all the containers in the system.

The Docker container Stop command can stop one or more containers, and we just need to list all the containers in the system that are running. Since Docker doesn't mind if we stop a stopped container again, simply brute point and list all the containers directly (including those that have been stopped)!

$ docker Container Ls-a-Q

-A displays all containers,-Q only displays the container ID in the form of a number.
Then take the result of the command execution here as a parameter to the Docker container Stop command:

$ docker Container Stop $ (Docker container ls-a-Q)
The complete command to restore the Docker environment is as follows:

$ docker Container Stop $ (Docker container ls-a-Q) && Docker system prune--all--force--volumns
Similar to the previous Prune command, you can also completely delete a class of resources:

Delete container: Docker container RM $ (Docker container ls-a-Q)
Delete Image: Docker image rm $ (docker image Ls-a-Q)
Delete Data Volume: Docker volume RM $ (Docker volume ls-q)
Delete Network:docker Network RM $ (Docker network Ls-q)

Create a shell alias
The above commands can accomplish tasks but are cumbersome, and we can simplify the execution of these commands through the Shell's alias function.

Alias Docker-clean-unused= ' docker system prune--all--force--volumes ' Alias Docker-clean-all= ' Docker stop $ (docker Container ls-a-Q) && Docker system prune--all--force--volumes '
Write the above command to the user's ~/.BASHRC file!

Perform a cleanup task:

Summarize
Regular cleanup of system resources not only allows the system to run more smoothly, but also helps us focus on the relevant key resources. So we recommend that you use the relevant resource cleanup commands to keep Docker fresh and efficient.

Oh yes, don't forget to look at it.

How to quickly clean up Docker useless resources

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.