In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to clean up the disk space occupied by Docker. I hope you will get something after reading this article. Let's discuss it together.
Docker takes up a lot of space, and whenever we run containers, pull images, deploy applications, and build our own images, we take up a lot of disk space.
If you are also troubled by this problem, let's take a look at how Docker uses disk space and how to recycle it.
The space occupied by docker can be viewed with the following command:
$docker system df
TYPE lists four types of disks used by docker:
Images: the space occupied by all images, including pulled images, and locally built images.
Containers: the space occupied by the running container, representing the space in the read-write layer of each container.
Local Volumes: the space in which the container mounts the local data volume.
Build Cache: the cache space generated during image construction (available only when using BuildKit, available after Docker 18.09).
The final RECLAIMABLE is the recyclable size.
Let's take a look at these types.
Disk usage of the container
Every time a container is created, some files and directories are created, such as:
/ var/lib/docker/containers/ID directory, if the container uses the default log mode, all its logs will be saved as JSON to this directory.
The / var/lib/docker/overlay2 directory contains the read-write layer of the container, which will be written to this directory if the container uses its own file system to store the data.
Now let's start with a completely clean system, assuming that docker has just been installed:
First, we start a NGINX container:
Now, after running the df command, you will see:
A mirror image, 126MB
A container.
There is no recyclable space at this time because the container is running and the image is being used.
Now, let's create an empty file for 100MB in the container:
$docker exec-ti www\ dd if=/dev/zero of=test.img bs=1024 count=0 seek=$ [1024,100]
View the space again:
You can see that the space occupied by the container has increased. Where is this file saved on this machine?
As mentioned above, it is stored in the read-write layer of the container.
When the container is stopped, the space occupied by the container becomes recyclable:
How to recycle it? Deleting a container removes the space occupied by its associated read-write layer.
You can also delete all stopped containers with one click:
$docker container prune
After deleting the container, the image can also be recycled:
The above docker container prune command deletes stopped containers. If you want to delete all containers (including stopped and running containers), you can use the following two commands:
$docker rm-f $(docker ps-aq) $docker container rm-f $(docker container ls-aq)
Mirrored disk usage
Some of the images are invisible:
Sub-images, which are intermediate images referenced by other images, cannot be deleted.
A suspended mirror, that is, a mirror that is no longer used, can be deleted.
The following command lists the mirrors of all suspended states:
$docker image ls-f dangling=true
Delete such images:
$docker image rm $(docker image ls-f dangling=true-Q)
Or:
$docker image prune
If you want to delete all mirrors, you can use the following command:
$docker image rm $(docker image ls-Q)
Note that the image being used by the container cannot be deleted.
Disk usage of data volum
A data volume is a data store outside the container's own file system.
For example, applications in the container can upload images, which cannot be saved inside the container after upload, because the data inside the container will be deleted with the death of the container. Therefore, these images should be kept outside the container, that is, data volumes.
For example, we run a MongoDB container for testing and import a lot of test data, which is not in the container, but in the data volume, because the data volume is used in MongoDB's Dockerfile.
After the test is complete, the MongoDB container is deleted, but the test data is still there and has not been deleted.
Delete data volumes that are no longer in use:
$docker volume rm $(docker volume ls-Q)
Or:
$docker volume prune
Disk usage of Build Cache
Docker 18.09 introduces BuildKit to improve the performance, security, storage management and other capabilities of the build process.
You can use the command to delete a build cache:
$docker builder prune
One click to clean up
From the above instructions, we know that containers, mirrors, and data volumes all provide prune subcommands to help us reclaim space.
In fact, there is also a prune subcommand at the docker system level, which can clean up useless space with one click:
$docker system prune
It is a good habit to carry out this order regularly.
After reading this article, I believe you have a certain understanding of "how to clean up the disk space occupied by Docker". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.