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

Who used up the disk? Detailed explanation of Docker System command

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

Share

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

Docker images, containers, data volumes, and networks all take up the host's disk space, so the disk can easily be used up. This blog introduces a simple solution-the Docker System command.

Original: What's eating my disk? Docker System Commands explained

Translator: Fundebug

In order to ensure readability, this paper adopts free translation rather than literal translation.

After using Docker for a while, you will find that it takes up a lot of hard disk space. Fortunately, Docker 1.13 introduces a workaround, which provides simple commands to view / clean up the disk space used by Docker.

This article uses a simple example to prove that Docker can quickly fill up disks. The example is run through play-with-docker.com. Click Add new instance to create a new instance with the latest version of Docker 17.03 installed. This blog focuses on disk space, so use the df command to check the initial state of the disk:

$df-hFilesystem Size Used Available Use% Mounted on/dev/mapper/... 10.0G 443.3M 9.6G 4% / tmpfs 60.0G 060.0G 0% / devtmpfs 60.0G 060.0G 0% / sys/fs/cgroup/dev/xvda1 49.1G 3. 7G 43.3G 8% / etc/resolv.conf/dev/xvda1 49.1G 3.7G 43.3G 8% / etc/hostname/dev/xvda1 49.1G 3.7G 43.3G 8% / etc/hostsshm 64.0M 064.0M 0% / dev/shm/dev/mapper/... 10.0G 443.3M 9.6G 4% / graph/overlay2

You can see that in the newly created play-with-docker.com instance, there is a total 10GB disk space, in which the proximity to 500MB has been occupied.

Next, write Dockerfile to create a mirror. This image is based on the Alpine image; the image will be written to three random files, each file 1GB, generated by the dd command; because this image has no actual effect, CMD is set to / bin/true.

FROM alpineRUN dd if=/dev/zero of=1g1.img bs=1G count=1RUN dd if=/dev/zero of=1g2.img bs=1G count=1RUN dd if=/dev/zero of=1g3.img bs=1G count=1CMD / bin/true

Run docker build-t test. An image can be created, and an image of 3GB will be generated when the execution is completed.

$docker image lsREPOSITORY TAG CREATED SIZEtest latest 38 seconds ago 3.23GBalpine latest 5 weeks ago 3.99MB

It is not difficult to understand that the mirror takes up the appropriate amount of disk space.

$df-hFilesystem Size Used Available Use% Mounted on/dev/mapper/... 10.0G 3.4G 6.5G 34% /

If you write only 2 random files, you need to modify the Dockerfile and delete one line. To avoid using caching when building the image, I added a line of echo command before the dd command.

FROM alpineRUN echo fooRUN dd if=/dev/zero of=1g1.img bs=1G count=1RUN dd if=/dev/zero of=1g2.img bs=1G count=1# RUN dd if=/dev/zero of=1g3.img bs=1G count=1CMD / bin/true

I thought this would save 1GB disk space, but the actual situation is even worse!

$df-hFilesystem Size Used Available Use% Mounted on/dev/mapper/... 10.0G 5.4G 4.5G 54% /

The old Docker image exists all the time, and eventually the disk space will be used up quickly. Docker 1.13 introduced the docker system df command, which is similar to the df command on Linux, to view the disk usage of Docker.

$docker system dfTYPE TOTAL ACTIVE SIZE RECLAIMABLEImages 3 0 5.373GB 5.373GB (100%) Containers 000B 0BLocal Volumes 000B

You can see that there are a total of three Docker images on the instance: the apline image, which contains three 1GB random files and the one containing two 1GB random files. These mirrors take up more than 5GB disk space. Since we are not running containers based on these images, they can all be deleted, so the RECLAIMABLE disk space is 100%. Use docker run test to run the test image and then view:

$docker system dfTYPE TOTAL ACTIVE SIZE RECLAIMABLEImages 3 1 5.373GB 3.225GB (60%) Containers 100B 0BLocal Volumes 000B 0B

Things are different now. I run a container that exits quickly after / bin/true. This container is bound with a test image, and the test image is marked as active and cannot be deleted, resulting in less disk space to be recycled.

Now let's clean up the disk space. Docker provides docker system prune, which can be used to clean up dangling images (see What are Docker: images?) And containers, as well as failed data volumes and networks.

$docker system pruneWARNING! This will remove:-all stopped containers-all volumes not used by at least one container-all networks not used by at least one container-all dangling imagesAre you sure you want to continue [y/N] yDeleted Containers:1cdf866157b4a97e151125af3c2a7f186a59b6f63807e2014ce1a00d68f44e1dDeleted Images:deleted: sha256:f59bb277...deleted: sha256:695b8e70...deleted: sha256:93b1cceb...deleted: sha256:c74d6bcd...deleted: sha256:df8b9bb1...deleted: sha256:dfe8340f...deleted: sha256:ce1ee654...Total reclaimed space: 3.221GB

According to the warning message, this command removes all closed containers and dangling images. In the example, the name of the image containing three random 1GB files is occupied, and the name is dangling image, so it is deleted. At the same time, all intermediate images will also be deleted. In that case, a total of 3GB disk space is recycled!

Further, use the-an option to do deep cleanup. At this point we will see a more serious WARNING message:

$docker system prune-aWARNING! This will remove:-all stopped containers-all volumes not used by at least one container-all networks not used by at least one container-all images without at least one container associated to themAre you sure you want to continue [y/N] yDeleted Images:untagged: test:latestdeleted: sha256:c515ebfa2...deleted: sha256:07302c011...deleted: sha256:37c0c6474...deleted: sha256:5cc2b6bc4...deleted: sha256:b283b9c35...deleted: sha256:8a8b9bd8b...untagged: alpine:latestuntagged: alpine@sha256:58e1a1bb75db1...deleted: sha256:4a415e366...deleted: sha256:23b9c7b43...Total reclaimed space: 2.151GB

This command will clean up the entire system and keep only the images, containers, data volumes, and networks that are actually in use, so you need to be very careful. For example, we cannot run the prune-a command in a production environment because some standby images (for backup, rollback, etc.) are sometimes needed, and if these images are deleted, you need to download them again when you run the container.

At this point, all unbound container images will be deleted. Because all containers are deleted by the first prune command, all images (they are not bound to any containers) are deleted.

$df-hFilesystem Size Used Available Use% Mounted on/dev/mapper/... 10.0G 442.5M 9.6G 4% /

Now, the disk space used is 4% again. The example in this article is just the tip of the iceberg, because once we run a real container and use Docker volumes and Docker networks, disk space will run out faster. If you are interested, you can check out the last video of the blog (don't forget to subscribe!) . In the video, I introduce a simple WordPress application that consists of several containers, data volumes and a network. This application can consume disk space very quickly, and I will show you how to deal with this problem.

Video: What's eating my diskhands Clean up your Docker System

About Fundebug

Fundebug specializes in real-time BUG monitoring of JavaScript, WeChat Mini Programs, Wechat, Mini Game, Mini Program, React Native, Node.js and Java. Since the official launch of Singles' Day in 2016, Fundebug has handled a total of 700 million + errors, which has been recognized by many well-known users, such as Google, 360,360, Kingsoft, people's Network and so on. Welcome to try it for free!

Copyright notice

Please indicate the author's Fundebug and the address of this article when reprinting:

Https://blog.fundebug.com/2017/04/19/docker-system-explain/

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