How to clean up the disk space occupied by Docker

How to clean up the disk space occupied by Docker

Docker takes up a lot of space. Whenever we run containers, pull images, deploy applications, and build our own images, our disk space will be occupied a lot.

If you are also troubled by this problem, let's take a look at how Docker uses disk space and how to reclaim it.

The space occupied by docker can be viewed through the following command:

$ docker system df 

TYPE lists the four types of disks used by Docker:

  • Images: The space occupied by all images, including those pulled down and those built locally.
  • Containers: The space occupied by running containers, indicating the space of the read-write layer of each container.
  • Local Volumes: The space where the container mounts local data volumes.
  • Build Cache: Cache space generated during the image building process (only available when using BuildKit, available after Docker 18.09).

The final RECLAIMABLE is the reclaimable size.

Let’s take a look at each of these types.

Container disk usage

Every time a container is created, some files and directories are created, for example:

  • /var/lib/docker/containers/ID directory. If the container uses the default log mode, all its logs will be saved in this directory in JSON format.
  • The /var/lib/docker/overlay2 directory contains the container's read-write layer. If the container uses its own file system to save data, it will be written to this directory.

Now we start from a completely clean system, assuming Docker has just been installed:

First, we start an NGINX container:

Now when you run the df command, you will see:

  • One image, 126MB
  • A container

There is no reclaimable space at this time because the container is running and the image is being used.

Now, we create an empty file of 100MB inside the container:

$ docker exec -ti www \
 dd if=/dev/zero of=test.img bs=1024 count=0 seek=$[1024*100]

Check the space again:

You can see that the space occupied by the container has increased. Where is this file saved on the local 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 reclaimable:

How to recycle? Deleting a container deletes 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 docker container prune command above deletes stopped containers. If you want to delete all containers (including stopped and running ones), you can use the following two commands:

$ docker rm -f $(docker ps -aq)

$ docker container rm -f $(docker container ls -aq)

Mirror disk usage

Some images are invisible:

  • A sub-image is an intermediate image referenced by other images and cannot be deleted.
  • A suspended image is an image that will no longer be used and can be deleted.

The following command lists all pending images:

$ 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 images, you can use the following command:

$ docker image rm $(docker image ls -q)

Note that images that are being used by containers cannot be deleted.

Disk usage of data volumes

A data volume is a data storage outside of the container's own file system.

For example, if the application in the container has the function of uploading pictures, the pictures cannot be saved inside the container after uploading, because the data inside the container will be deleted when the container dies. Therefore, these pictures must be saved outside the container, that is, in the data volume.

For example, we run a MongoDB container for testing and import a lot of test data. This data is not in the container, but in the data volume, because the data volume is used in the MongoDB Dockerfile.

After the test is completed, the MongoDB container is deleted, but the test data is still there and has not been deleted.

Delete the data volume that is no longer in use:

$ docker volume rm $(docker volume ls -q)

or:

$ docker volume prune 

Build Cache Disk Space

Docker 18.09 introduced BuildKit, which improves the performance, security, storage management and other capabilities of the build process.

To delete the build cache, you can use the command:

$ docker builder prune 

One-click cleaning

From the above description, we know that containers, images, and data volumes all provide the prune subcommand 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 execute this command regularly.

Translated from:

https://medium.com/better-programming/docker-tips-clean-up-your-local-machine-35f370a01a78

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • About Docker's cleanup of Overlay2's occupied disk space (tested and effective)
  • Solution to Docker disk space cleaning
  • Solve the problem of insufficient docker disk space
  • How to analyze and clean up Docker disk space usage
  • Solution to the problem of Docker taking up all the disk space
  • Solution to the problem of insufficient disk space and inaccessibility caused by Docker container

<<:  WeChat applet implements calculator function

>>:  MySQL establishes efficient index example analysis

Recommend

Detailed explanation of replace into example in mysql

Detailed explanation of replace into example in m...

React example of how to get the value of the input box

React multiple ways to get the value of the input...

JS quickly master ES6 class usage

1. How to construct? Let's review the common ...

How to implement Mysql switching data storage directory

How to implement Mysql switching data storage dir...

Today I encountered a very strange li a click problem and solved it myself

...It's like this, today I was going to make a...

XHTML tags have a closing tag

<br />Original link: http://www.dudo.org/art...

3 simple ways to achieve carousel effects with JS

This article shares 3 methods to achieve the spec...

A detailed introduction to setting up Jenkins on Tencent Cloud Server

Table of contents 1. Connect to Tencent Cloud Ser...

Sample code for using CSS to write a textured gradient background image

The page length in the project is about 2000px or...

Installation of CUDA10.0 and problems in Ubuntu

The correspondence between tensorflow version and...

Detailed troubleshooting of docker.service startup errors

Execute the following command to report an error ...

Examples of using html unordered list tags and ordered list tags

1. Upper and lower list tags: <dl>..</dl...