docker clean up disk space

Clean up the disk space occupied by Docker

        </h1>
        <div class="clear"></div>
        <div class="postBody">

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 used up a lot.
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 by the following command:

$ docker system df

TYPE lists 4 types of disks used by docker:

  • Images : The space occupied by all the images, including the extracted images, and locally built ones.

  • Containers : The space occupied by running containers, representing the space of the read-write layer of each container.

  • Local Volumes : Space for containers to mount local data volumes.

  • Build Cache : Cache space generated during the image build process (only available when using BuildKit, available after Docker 18.09).
    The last RECLAIMABLE is the recyclable size.
    Let's take a look at these types separately.
    Container disk usage
    Each 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 his 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 with a completely clean system, assuming that docker has just been installed:

    first, we start an NGINX container:

Now after running the df command, you will see:
a mirror, 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 a 100MB empty file in 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 stored 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? When a container is deleted, the space occupied by its associated read-write layer is deleted.
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 is to delete the stopped container. If you want to delete all containers (including stopped and running), 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-mirror is an intermediate mirror that is referenced by other mirrors and cannot be deleted.

  • The mirror in the suspended state, that is, the mirror that will not be used anymore, can be deleted.
    The following command lists all suspended mirrors:

$ docker image ls -f dangling=true

Delete such mirrors:

$ docker image rm $(docker image ls -f dangling=true -q)
或者:
$ docker image prune

If you want to delete all mirrors, you can use the following command:
do c k e r i m a germdockerimagerm (docker image ls -q)
Note that theimagebeing used by the container cannot be deleted.

Data volume disk usage

The data volume is the data storage outside the file system of the container itself.
For example, the application in the container has the function of uploading pictures. After uploading, it must not be saved in the container, because the data in the container will be deleted as the container dies, so these pictures should be saved outside the container, that is, data volume.
For example, we ran a MongoDB container for testing and imported a lot of test data. These data are not inside the container, but in the data volume, because the data volume is used in the MongoDB Dockerfile.
After the test was completed, the MongoDB container was deleted, but the test data was still there and was not deleted.

Delete data volumes that are no longer in use:

$ docker volume rm $(docker volume ls -q)
或者:
$ docker volume prune

Build Cache disk usage

Docker 18.09 introduced BuildKit, which improves the performance, security, storage management and other capabilities of the build process.
You can use the command to delete the build cache:

$ docker builder prune

One-click cleaning

Through the above instructions, we know that the container, mirror, and data volume all provide the prune subcommand to help us reclaim space.
In fact, there is also the 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.

Published 63 original articles · praised 7 · views 3396

Guess you like

Origin blog.csdn.net/weixin_44523062/article/details/105381806