Docker 垃圾回收机制补充
作者:张首富
时间:2020-07-19
w x:y18163201
我们在之前的文章里面讲过Docker 垃圾回收机制里面简单的介绍了下docker * prune
命令,今天我们来详细的解读下最后一个docker system prune
指令,解读这个命令之前我们先来了解下docker system df
docker system df
此参数要在 client 和 service 端的版本:1.25 版本以上才能使用
是做什么的?
这个命令是输出 Docker 在宿主机上资源的使用情况。可以查看镜像占用空间大小,容器占用空间大小,本地的volume 和 build 缓存的大小
举例使用
创建模拟环境
# docker pull centos:7.6.1810
# docker pull busybox
# docker pull sellbot:20200617
# docker pull sellbot:20200614
# docker pull sellbot:20200613
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/smb/sellbot 20200716 f363f6d1f3f5 3 days ago 544MB
registry.cn-hangzhou.aliyuncs.com/smb/sellbot 20200714 3b4aa15b779d 4 days ago 544MB
registry.cn-hangzhou.aliyuncs.com/smb/sellbot 20200713 1f7133d6f068 6 days ago 544MB
busybox latest c7c37e472d31 2 weeks ago 1.22MB
centos 7.6.1810 f1cb7c7d58b7 16 months ago 202MB
# docker run -id --name test centos:7.6.1810 /bin/bash -c "dd if=/dev/zero of=/tmp/1.txt bs=10000 count=2000 && sleep 300 "
# docker run -id busybox /bin/sh -c "/bin/sleep 36000"
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b2fa31af4348 centos:7.6.1810 "/bin/bash -c 'dd if…" About a minute ago Up About a minute test
7bbaea0e2ded busybox "/bin/sh -c '/bin/sl…" 2 minutes ago Up 2 minutes clever_rubin
然后使用命令查看磁盘的使用情况,
# docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 5 2 797.9MB 595MB (74%)
Containers 5 1 20MB 20MB (100%)
Local Volumes 0 0 0B 0B
Build Cache 0 0 0B 0B
如果什么参数不加这显示数据的摘要信息,来解释下每列的含义:
1, Type: 显示占用磁盘空间的类型
2, Total: 当前类型的总数量,比如在我们这个环境一共有 5 个镜像,
3, ACTIVE: 处于活动中的,当前环境中有两个镜像正在使用,
4, SIZE:当前镜像,容器占用磁盘的大小,
5,RECLAIMABLE:可回收,可以被释放的,百分比就是 RECLAIMABLE/SIZE 的大小。
我们可以查看下每一项的详细信息
# docker system df -v
Images space usage:
REPOSITORY TAG IMAGE ID CREATED SIZE SHARED SIZE UNIQUE SIZE CONTAINERS
registry.cn-hangzhou.aliyuncs.com/smb/sellbot 20200716 f363f6d1f3f5 3 days ago 543.9MB 518.4MB 25.51MB 0
registry.cn-hangzhou.aliyuncs.com/smb/sellbot 20200714 3b4aa15b779d 4 days ago 543.9MB 518.4MB 25.51MB 0
registry.cn-hangzhou.aliyuncs.com/smb/sellbot 20200713 1f7133d6f068 6 days ago 543.9MB 518.4MB 25.51MB 0
busybox latest c7c37e472d31 2 weeks ago 1.224MB 0B 1.224MB 4
centos 7.6.1810 f1cb7c7d58b7 16 months ago 201.8MB 0B 201.8MB 1
Containers space usage:
CONTAINER ID IMAGE COMMAND LOCAL VOLUMES SIZE CREATED STATUS NAMES
b2fa31af4348 centos:7.6.1810 "/bin/bash -c 'dd if…" 0 20MB 12 minutes ago Exited (0) 7 minutes ago test
7bbaea0e2ded busybox "/bin/sh -c '/bin/sl…" 0 0B 12 minutes ago Up 12 minutes clever_rubin
ed33045b3f09 busybox "/bin/sh -c /bin/sle…" 0 0B 13 minutes ago Created zen_turing
fba6d39f9b6a busybox "/bin/sh -c /bin/sle…" 0 0B 13 minutes ago Exited (1) 13 minutes ago pensive_lederberg
6c2e7a0b4e7a busybox "sh" 0 18B 14 minutes ago Exited (0) 14 minutes ago priceless_mirzakhani
Local Volumes space usage:
VOLUME NAME LINKS SIZE
Build cache usage: 0B
CACHE ID CACHE TYPE SIZE CREATED LAST USED USAGE SHARED
我们可以看到当我们添加了-v
选项之后就能看到更多的信息了,相信喜欢观察的您一定发现了在 images 这一列有一个有意思的现象,就是 SIZE = SHARED SIZE + UNIQUE SIZE
我们来分开介绍每一个表格里面每一列的含义
Images space usage::
- REPOSITORY: 镜像名称,由 仓库地址/存放空间/镜像名称 组成
- TAG: 当前一个镜像的 tag,用来区分同一个镜像的不同版本
- IMAGE ID: 镜像 ID,用来区别是否为同一个镜像(包括版本)
- CREATE: 当前这个镜像的创建时间,可以理解为构建时间
- SIZE: 当前这个镜像的大小
- SHARED SIZE: 共享层的大小,我们都知道 Docker 是分层存储的,相同层只存储一份
- UNIQUE SIZE:区别于其他镜像的不同层的大小
- CONTAINERS:当前有几个容器是基于这个镜像启动的。镜像是否能被清理依赖于这个
Containers space usage:
- CONTAINER ID: 容器 ID
- IMAGE: 容器依赖于那个镜像启动的
- COMMAND: 容器的启动命令是什么
- LOCAL VOLUMES: VOLUME 的数量
- SIZE: 当前容器的大小,指的是可写层的大小,并不包含镜像的大小
- CREATED: 创建时间
- STATUS:当前容器的状态
- NAMES:当前容器的名称
Local Volumes space usage:
- VOLUME NAME: 卷的名称
- LINKS:
- SIZE:大小
Build cache usage: 0B
在我们这个例子里面只有 images 和 containers 有大小,是因为我们没有 build 过任何镜像,也没有把容器的时候挂载数据卷
注意:
这个数据卷并不是-v 指定的数据卷,而是我们在构建 Dockerfile 的时候写的 VOLUME
我们下面来做个实验,把 VOLUME 弄出来
FROM centos:7.6.1810
MAINTAINER zhangshoufu(wx:y18163201)
VOLUME /test/data
构建 Docker 镜像
# docker build . -t centos:test_volume -f Dockerfile
# docker images | grep volume
centos test_volume 4c3708d6ffdd 16 seconds ago 202MB
启动
# docker run -id --name test-volume-size -v /tmp/test:/test centos:test_volume /bin/bash -c "dd if=/dev/zero of=/test/data/1.txt bs=10000 count=2000 && sleep 300 "
查看
# docker system df -v | grep -A 3 "VOLUME NAME"
VOLUME NAME LINKS SIZE
c72d1b4b1e06d564e6d2ecb19de0daddbfbfa322a1b7545b5b1f8b418a2ecb46 1 20MB
到此我们就给 Docker system df 命令讲解完成了,知道这些信息,我们自定义监控的时候就能获取到一些特定的值,
docker system prune 删除未使用的数据卷
它能做什么
它能删除所有未使用的容器,网络,镜像(悬空和未引用),以及卷
不加参数使用它看看是什么效果
# docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all dangling build cache
Are you sure you want to continue? [y/N] y
Deleted Containers:
579f5e4e70638daa21e0b781bf3c394798df9bd70ceff0ba970d574961c74a4c
f9725e158430855bf5e77ec04b66f6edb7eb59c24bd122457ff7a167381e7599
b2fa31af4348519fa4cdd8270a08498b6c771ece51bc14b3caccacdfd3ad671b
ed33045b3f09eb3c245e1d49214420a8cd0e1a594a85c2323112a77d6006590c
fba6d39f9b6a022472bd9c0083972dcb220a946924be8727a94b89f1a419872c
6c2e7a0b4e7a0517970c465b4b53af8b630fe3255ddab11fa1fb2144cb540d3f
Total reclaimed space: 20MB
然后我们 docker system df -v 看下结果
# docker system df -v
Images space usage:
REPOSITORY TAG IMAGE ID CREATED SIZE SHARED SIZE UNIQUE SIZE CONTAINERS
centos test_volume 4c3708d6ffdd 9 minutes ago 201.8MB 201.8MB 0B 0
registry.cn-hangzhou.aliyuncs.com/smb/sellbot 20200716 f363f6d1f3f5 3 days ago 543.9MB 518.4MB 25.51MB 0
registry.cn-hangzhou.aliyuncs.com/smb/sellbot 20200714 3b4aa15b779d 5 days ago 543.9MB 518.4MB 25.51MB 0
registry.cn-hangzhou.aliyuncs.com/smb/sellbot 20200713 1f7133d6f068 6 days ago 543.9MB 518.4MB 25.51MB 0
busybox latest c7c37e472d31 2 weeks ago 1.224MB 0B 1.224MB 1
centos 7.6.1810 f1cb7c7d58b7 16 months ago 201.8MB 201.8MB 0B 0
Containers space usage:
CONTAINER ID IMAGE COMMAND LOCAL VOLUMES SIZE CREATED STATUS NAMES
7bbaea0e2ded busybox "/bin/sh -c '/bin/sl…" 0 0B 3 hours ago Up 3 hours clever_rubin
Local Volumes space usage:
VOLUME NAME LINKS SIZE
c72d1b4b1e06d564e6d2ecb19de0daddbfbfa322a1b7545b5b1f8b418a2ecb46 0 20MB
Build cache usage: 0B
CACHE ID CACHE TYPE SIZE CREATED LAST USED USAGE SHARED
从上面可以看出来他给我们失败的 docker,或者说停止的容器全部都清除了,释放了容器可写层的大小,镜像并未删除,
注意:
docker system prune 默认只会删除失败的或者停止的容器,和他们的可层,也会删除悬空的镜像,并不会清除所有镜像。
我们可以使用 --filter 参数清除哪一类容器,当前支持的参数
The currently supported filters are:
- until (
<timestamp>
) - only remove containers, images, and networks created before given timestamp- label (
label=<key>
,label=<key>=<value>
,label!=<key>
, orlabel!=<key>=<value>
) - only remove containers, images, networks, and volumes with (or without, in caselabel!=...
is used) the specified labels.
我们可以使用-a 参数清除当前机器上没有被引用的镜像
# docker system prune -a
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all images without at least one container associated to them
- all build cache
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: registry.cn-hangzhou.aliyuncs.com/smb/sellbot:20200714
untagged: registry.cn-hangzhou.aliyuncs.com/smb/sellbot@sha256:5a384898e7d11d99cefc55d9cd63518233b9751a5c4c465526cb14fff93985fd
deleted: sha256:3b4aa15b779d469887b8477b53d7cac5f4e8373ab39d0de015dec4c952ed8f87
deleted: sha256:6619936848bb2c35b219c1b2726c09fa6e8e54cb51449b6654ceeb45460236f7
untagged: registry.cn-hangzhou.aliyuncs.com/smb/sellbot:20200713
untagged: registry.cn-hangzhou.aliyuncs.com/smb/sellbot@sha256:f56d67fb69575ac79b48eb8f7a1f112966d01b6e80c1e89adaa454c8c686ca3d
deleted: sha256:1f7133d6f068499f91366eef403ed29478a9292720a3d59a8eb2f481560121aa
deleted: sha256:28044d3747ae44664b37ca14d99afe450f73e2ef05b960279f821b231a5cca6b
untagged: centos:test_volume
deleted: sha256:4c3708d6ffdd898ec25728870ad3c97bd27cbaaf9637b1b45a4e85e5a92095a9
deleted: sha256:a9d487236d0a89428a695de3a1c340c5e740bf213a35dcd9a8053025733531cc
untagged: registry.cn-hangzhou.aliyuncs.com/smb/sellbot:20200716
untagged: registry.cn-hangzhou.aliyuncs.com/smb/sellbot@sha256:b723f501cdebcee79b4d6b5b0859ce20718eded417928fa18d9f3b0135bc3ee4
deleted: sha256:f363f6d1f3f5fd25cedc0824fb33d17b5109d96a8a14a7931c7da8de812c37f6
deleted: sha256:16f95c955f85d8e1d5e6b3810e4843b9828bb0f5e215817661648b6d9c5a75b3
deleted: sha256:935e843cce2d562611a950de31beb63e17a9a1f3c3421ae3cab8db7f7ce3e1e0
deleted: sha256:bcf75aa1d1da5a81575b499af40a578888a8a7e0f813e0f516d7aefebc13690b
deleted: sha256:9ad3e1811704d30494e2d402d432c00d1d9153784a9b31cc058fffc672557606
deleted: sha256:a3473da06c6e367bb9ecb640ecac13d916568324a15ae7e8fed8f465e0690464
deleted: sha256:4a7021bd2ec6699a4c2a3c7df7ef9197b678ad56193b5b1d309a10d89532666d
deleted: sha256:6c3b476844a247d18c7eb535b9cc9d3d9f6d5063691cac5411f254b9fd7798f2
deleted: sha256:734e667e23a00543dbc5617963cb1473651316c0b1c0ba412e472960318d6e04
deleted: sha256:ffc9b21953f4cd7956cdf532a5db04ff0a2daa7475ad796f1bad58cfbaf77a07
untagged: centos:7.6.1810
untagged: centos@sha256:62d9e1c2daa91166139b51577fe4f4f6b4cc41a3a2c7fc36bd895e2a17a3e4e6
deleted: sha256:f1cb7c7d58b73eac859c395882eec49d50651244e342cd6c68a5c7809785f427
deleted: sha256:89169d87dbe2b72ba42bfbb3579c957322baca28e03a1e558076542a1c1b2b4a
Total reclaimed space: 796.7MB
[root@zhangsf docker]# docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 1 1 1.224MB 0B (0%)
Containers 1 1 0B 0B
Local Volumes 1 0 20MB 20MB (100%)
Build Cache 0 0 0B 0B
但是并不会清除 VOLUME,如果我们想清除 VOLUME,可以使用下面参数,但是不建议使用
# docker system prune -f --volumes
Deleted Volumes:
c72d1b4b1e06d564e6d2ecb19de0daddbfbfa322a1b7545b5b1f8b418a2ecb46
Total reclaimed space: 20MB