How to clean up disk space occupied by Docker

Docker very space, whenever we run container, pull the mirror, when you deploy the application, build your own mirror, we will be a lot of disk space occupied.

If you have been plagued by this problem, we will go together to see how Docker is the use of disk space, and how to recycle.

docker occupied space can be viewed through the following command:

$ docker system df

TYPE Lists four types docker disk usage:

  • Images : All the space occupied by the mirror, the mirror comprising a pulling down, and the local construction.
  • Containers : running the space occupied by the container, each layer represents a write space of the container.
  • Volumes the Local : a container mount space of the local data volume.
  • The Cache the Build : Construction of buffer space image generated in the process (only have, Docker 18.09 later available using BuildKit).

The final RECLAIMABLEis recyclable size.

Here are a look at these types.

Disk container occupancy

Each time you create a container, there will be a number of files and directories are created, for example:

  • /var/lib/docker/containers/IDDirectory, if the container is used the default logging mode, all of his logs will be saved to this directory in JSON format.

  • /var/lib/docker/overlay2 Containing layer read-write directory container, if the container uses its own file system to store data, it will be written in this directory.

Now we start from a totally clean system, assuming docker just installed:

First, we start a NGINX container:

Now run the dfcommand, you will see:

  • A mirror, 126MB
  • A container

In this case there is no space recyclable since the container in operation, the mirror is being used.

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

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

View space again:

You can see the space occupied by the container increases, the file is saved in the machine where is it?

And as said above, it is stored in the read-write layer container.

When the container is stopped, the space occupied by the container will become recyclable:

How to recycle it? It will remove the space occupied by its associated reading and writing layer when a container is removed.

It can also be a key to remove all the containers have been stopped:

$ docker container prune

删除容器后,镜像也可以回收了:

上面的 docker container prune 命令是删除停止的容器,如果想删除所有容器(包括停止的、正在运行的),可以使用下面这2个命令:

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

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

镜像的磁盘占用

有一些镜像是隐形的:

  • 子镜像,就是被其他镜像引用的中间镜像,不能被删除。
  • 悬挂状态的镜像,就是不会再被使用的镜像,可以被删除。

下面的命令列出所有悬挂状态的镜像:

$ docker image ls -f dangling=true

删除这类镜像:

$ docker image rm $(docker image ls -f dangling=true -q)

或者:

$ docker image prune

如果想删除所有镜像,可以使用下面的命令:

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

注意,正在被容器使用的镜像是不能被删除的。

数据卷的磁盘占用

数据卷是容器自身文件体统之外的数据存储。

例如容器中的应用有上传图片的功能,上传之后肯定不能保存在容器内部,因为容器内部的数据会随着容器的死掉而被删除,所以,这些图片要保存在容器之外,也就是数据卷。

比如我们运行了一个 MongoDB 容器做测试,导入了很多测试数据,这些数据就不是在容器内部的,是在数据卷中,因为 MongoDB 的 Dockerfile 中使用了数据卷。

测试完成后,删除了这个 MongoDB 容器,但测试数据还在,没被删除。

删除不再使用的数据卷:

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

或者:

$ docker volume prune

Build Cache 的磁盘占用

Docker 18.09 引入了 BuildKit,提升了构建过程的性能、安全、存储管理等能力。

删除 build cache 可以使用命令:

$ docker builder prune

一键清理

通过上面的说明,我们知道了像容器、镜像、数据卷都提供了 prune这个子命令,帮助我们回收空间。

其实,docker 系统层面也有 prune 这个子命令,可以一键清理没用的空间:

$ docker system prune

定期执行这个命令是个好习惯。

翻译整理自:

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

推荐阅读:

Guess you like

Origin www.cnblogs.com/yogoup/p/12143103.html