Current location - Health Preservation Learning Network - Slimming men and women - Docker's resources are expanding so fast, do you know how to clean them up?
Docker's resources are expanding so fast, do you know how to clean them up?
If you often use docker, you will find that the resources occupied by docker are expanding rapidly, and the most obvious and easily perceived is the occupation of disk space. This article will introduce how to quickly clean up the system resources occupied by docker, specifically, delete those useless images, containers, networks and data volumes.

Before cleaning up the resources, it is necessary for us to find out which system resources docker occupies. This requires a combination of different commands.

Docker container ls: By default, only the running containers are listed, while the -a option lists all containers including stops.

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

Docker volume ls: lists data volumes.

Docker network ls: list network.

Docker info: Displays system-level information, such as the number of containers and mirrors.

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

Docker provides a convenient Docker system pruning command to delete stopped containers, hung images, networks not referenced by containers, and caches during construction:

& ltpre style = " margin:0px; Fill: 0px Blank: Pre-newline; Word wrap: hyphenation; Font series: "Express New"! Important; font-size: 12px! Important; ">$ docker system cleaning & lt/pre & gt;;

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

& ltpre style = " margin:0px; Fill: 0px Blank: Pre-newline; Word wrap: hyphenation; Font series: "Express New"! Important; font-size: 12px! Important; "> $ docker system prune-all-force-volumes <: /pre & gt;

This time, not only the amount of data is deleted, but also there is no confirmation process! Note that using the-all parameter will delete all unreferenced images, not just hanging images.

It is necessary to explain what a suspended image is. A suspended image can be simply understood as a mirror image that is not referenced by any mirror image. For example, after the mirror image is reconstructed, the previously constructed mirror layer that is no longer referenced will become a hanging image:

After the local image is updated, it is similar to

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

We can also execute prune under different subcommands to delete specific types of resources:

Docker container prune # deletes all containers that exit this state.

Docker Volume Cleanup # Delete unused data volumes.

Docker Image Trim # Deletes hanging or all unused images.

The "installation status" here refers to the resource occupation 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 the backup data of the production environment) in a clean docker environment for bug investigation. Let's see what needs to be done.

Recall the command prune-all-force-volumns of docker system we introduced earlier. If all containers in the system have stopped before executing this command, then this command will delete all resources! Ok, now let's try to stop all containers in the system.

Docker container stop command can stop one or more containers. We just need to list all the running containers in the system. Because docker doesn't mind that we stop a stopped container again, just list all the containers (including those that have stopped) simply and rudely!

& ltpre style = " margin:0px; Fill: 0px Blank: Pre-newline; Word wrap: hyphenation; Font series: "Express New"! Important; font-size: 12px! Important; & gt$ docker container ls-a-q <; /pre & gt;

-a displays all containers, while -q displays only the container ID in numerical form.

Then take the execution result of the command here as the parameter of the docker container stop command:

& ltpre style = " margin:0px; Fill: 0px Blank: Pre-newline; Word wrap: hyphenation; Font series: "Express New"! Important; font-size: 12px! Important; & gt(docker container ls-a-q) < /pre & gt;

The complete command to restore the docker environment is as follows:

& ltpre style = " margin:0px; Fill: 0px Blank: Pre-newline; Word wrap: hyphenation; Font series: "Express New"! Important; font-size: 12px! Important; & gt (terminal container ls-a-q); & ampdocker system prune-all-force-volumes < /pre & gt;

Similar to the previous cleanup command, you can also completely delete certain types of resources:

Delete container: docker container RM (docker image ls-a-q)

Delete data volume: docker volume RM (docker network ls-q)

The above command can complete the task, but it is very cumbersome. We can simplify the execution of these commands through the alias function of the shell.

& ltpre style = " margin:0px; Fill: 0px Blank: Pre-newline; Word wrap: hyphenation; Font series: "Express New"! Important; font-size: 12px! Important; & gt alias docker-clean-unused =' docker system prune-all-force-volumes' alias docker-clean-all =' docker stop $ (docker container ls-a-q)&; & ampdocker system trims all force rolls' & lt/pre & gt;;

Just write the above command into the user's ~/. Bashrc file!

Perform cleaning tasks:

Cleaning system resources frequently can not only make the system run more smoothly, but also help us focus on related key resources. Therefore, it is suggested that you can use related resource cleaning commands to keep docker fresh and efficient.

Reference:

Clean up your Docker images, containers and volumes with simple commands.

Python zero-based introductory course

This course is a face-to-face and online course,1* *15 class hours, one day a week, lasting for 4 months. Attachment: video recording+notes+Q&A will be held in September-June.

Advanced Python Automation Operation and Maintenance Course

This course is a face-to-face and online course,1* *15 class hours, one day a week, lasting for 4 months. Attachment: video recording+notes+question and answer 2065438+September-April.

Docker+K8s course

This course is an online course, 150 class hours, one day a week, lasting for 4 months. Attachment: Video recording+notes+Q&A has started now.

Register now and enjoy the early bird price.

Goran course

You can enjoy the early bird price if you register in advance.

Students who want to know more and register can scan the code to add private chat with friends.