Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to quickly clean up Docker resources

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to quickly clean up Docker resources. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

View the resources consumed by docker

Before we clean up the resources, it is necessary to find out which system resources are occupied by docker. This needs to be done with a combination of different commands.

Docker container ls: only running containers are listed by default, and the-an option lists all containers that include stops.

Docker image ls: lists the image information, and the-an option lists intermediate images (that is, the layers on which other images depend).

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 images.

After looking at the resources used by docker through these commands, I believe you have decided to clean up some of the resources used by docker! Let's start with the unused resources.

Delete only those resources that are not used

Docker provides convenient docker system prune commands to delete stopped containers, dangling images, network that are not referenced by the container, and cache during the build process:

$docker system prune

For security reasons, this command does not delete volumes that are not referenced by any container by default. If you need to delete these volumes at the same time, you need to explicitly specify the-volumns parameter. 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 there will be no confirmation process! Note that using the-- all parameter removes all unreferenced mirrors, not just dangling mirrors. It is necessary to explain what dangling images is, but it can be simply understood as an image that is not referenced by any image. For example, after you rebuild the mirror, the previously built mirror layers that are no longer referenced become dangling images:

After the local image is updated, there will be a mirror similar to the one in the red box in the picture. This means that the old images are no longer referenced and they become dangling images. If you use the-a parameter, you will also find another type of image whose repository and tag columns are shown as follows:

These mirrors are called intermediate images (that is, the layers on which other mirrors depend).

We can also execute prune under different subcommands, so that certain types of resources are deleted:

Docker container prune # Delete all containers in exit status

Docker volume prune # deleting unused data volumes

Docker image prune # Delete dangling or all unused images

Return docker to the state it was when it was installed

The term "status at installation" here refers to resource usage rather than the relevant configuration of docker. This is also a common use case. For example, the author needs to automatically restore a production environment (using backup data from the production environment) in a clean docker environment for bug investigation. Let's see what needs to be done. Recall that the docker system prune-- all-- force-- volumns command we introduced earlier will remove all resources if all the containers in the system have been stopped before executing this command! Okay, now let's find a way to stop all the containers in the system. The docker container stop command can stop one or more containers. We just need to list all the containers that are running in the system. Since docker doesn't mind if we stop a stopped container again, simply be rude and list all the containers (including those that have already stopped)!

$docker container ls-a-Q

-a displays all containers, and-Q shows only digital container ID. Then take the result of the execution of the command here as an argument to the docker container stop command:

$docker container stop $(docker container ls-a-Q)

The commands to fully restore the docker environment are as follows:

$docker container stop $(docker container ls-a-Q) & & docker system prune-- all-- force-- volumns

Similar to the previous prune command, you can delete a type of resource completely:

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 the task but are tedious, and we can simplify the execution of these commands through the alias function of shell.

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'

Just write the above command to the user's ~ / .bashrc file!

Perform a cleanup task:

This is the end of the article on "how to quickly clean up Docker resources". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report