目录

Docker system 命令教程:查看磁盘用量、清理数据与监控事件

docker system 可查看 Docker 数据用量、清理未使用数据,并获取系统信息与事件;清理时可参考清理硬盘空间了解如何管理 Docker 占用的空间。

语法

docker system df [OPTIONS]
docker system prune [OPTIONS]
docker info [OPTIONS]
docker events [OPTIONS]

docker info 与 docker system info、docker events 与 docker system events 分别是相同命令的别名。

参数

命令参数说明
docker system df--format 模板使用自定义模板输出;可用 table、json 或 Go 模板
docker system df-v, --verbose显示空间用量的详细信息
docker system prune-a, --all清除所有未使用镜像,而不只是清除悬空镜像
docker system prune--filter 条件按条件筛选清理对象,例如 label=<key>=<value>
docker system prune-f, --force不提示确认
docker system prune--volumes清理匿名卷
docker info-f, --format 模板使用自定义模板输出;可用 json 或 Go 模板
docker events-f, --filter 条件按指定条件筛选输出事件
docker events--format 模板使用自定义模板输出;可用 json 或 Go 模板
docker events--since 时间显示指定时间之后创建的事件
docker events--until 时间持续流式输出事件,直到指定时间

示例

查看磁盘用量和详细信息

$ docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          5         3         420.2MB   239.7MB (57%)
Containers      3         3         139.3kB   0B (0%)
Local Volumes   1         1         3.854MB   0B (0%)
Build Cache     0         0         0B        0B

$ docker system df -v | head -20
Images space usage:

REPOSITORY   TAG           IMAGE ID       CREATED         SIZE      SHARED SIZE   UNIQUE SIZE   CONTAINERS
myweb        snapshot      56e3f92534ca   7 seconds ago   92.1MB    0B            92.08MB       0
nginx        1.30-alpine   47928bf67eb6   3 days ago      93.5MB    12.94MB       80.6MB        1
registry     3             01cd312e6702   3 days ago      86.9MB    12.94MB       73.93MB       1
<none>       <none>        0bf6577d7774   3 days ago      161MB     12.93MB       147.6MB       0
alpine       3.23          ff7f0c727dd7   3 days ago      13MB      12.93MB       86.86kB       1

Containers space usage:

CONTAINER ID   IMAGE               COMMAND                  LOCAL VOLUMES   SIZE      CREATED          STATUS          NAMES
0c009f73010b   alpine:3.23         "sleep 600"              0               4.1kB     42 seconds ago   Up 41 seconds   small
63b9a2f9c63f   nginx:1.30-alpine   "/docker-entrypoint.…"   0               115kB     44 seconds ago   Up 43 seconds   web
35357aad74b1   registry:3          "/entrypoint.sh /etc…"   1               20.5kB    2 minutes ago    Up 2 minutes    registry

Local Volumes space usage:

VOLUME NAME                                                        LINKS     SIZE
21ea99090ddead0461eb5a6efd670e1d9cf58fe9c23d5f8618db5868fb6170aa   1         3.854MB
  • 常规摘要分别列出镜像、容器、本地卷和构建缓存的总数、活动数量、大小和可回收大小。
  • -v 会列出各镜像与容器的用量;镜像详细信息还包括共享大小、独占大小以及使用该镜像的容器数量。
  • head -20 只保留输出的前 20 行,因此这里没有显示后续详细内容。

清理未使用数据

$ docker system prune -f
Deleted Images:
untagged: sha256:0bf6577d7774e592b29d093e3ac9390eb91e13d72c21e506afce736de5e0eeb0
deleted: sha256:0bf6577d7774e592b29d093e3ac9390eb91e13d72c21e506afce736de5e0eeb0
deleted: sha256:16b124a2d3eacd620d917ead6fd26cbf1f1d7117fa94122398971931cfe2d403
deleted: sha256:f79a7876c6644b273e4eec899344e51c66959237a2f71188db8cb98d7311521c

Total reclaimed space: 147.6MB

$ docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          4         3         272.6MB   92.08MB (33%)
Containers      3         3         139.3kB   0B (0%)
Local Volumes   1         1         3.854MB   0B (0%)
Build Cache     0         0         0B        0B
  • -f 让清理命令跳过确认提示。示例删除了未使用的镜像数据,报告回收了 147.6MB。
  • 清理前后容器数都是 3;镜像从 5 个变为 4 个,总大小从 420.2MB 降至 272.6MB。
  • --all 可清理所有未使用镜像,--volumes 可清理匿名卷;请先确认需要保留的数据。

用格式模板获取系统信息

$ docker info --format '{{.ServerVersion}} {{.Driver}} {{.Containers}} containers, {{.Images}} images'
29.9.2 overlayfs 3 containers, 5 images
  • --format 让输出只包含模板指定的服务器版本、存储驱动程序、容器数和镜像数。
  • 示例显示服务器版本为 29.9.2、存储驱动程序为 overlayfs。

按时间和条件查看事件

$ timeout 3 docker events --since 30s --format '{{.Type}} {{.Action}} {{.Actor.Attributes.name}}' ; true
container top web
container top web
container kill busy
network disconnect bridge
container die busy
container destroy busy
image save sha256:47928bf67eb62a1863cb08d9ecb52d3e704304ba614eaee17884204677702951
image save sha256:47928bf67eb62a1863cb08d9ecb52d3e704304ba614eaee17884204677702951
image untag nginx:1.30-alpine
image load sha256:47928bf67eb62a1863cb08d9ecb52d3e704304ba614eaee17884204677702951
container exec_create: sh -c echo hello > /usr/share/nginx/html/hi.txt web
container exec_start: sh -c echo hello > /usr/share/nginx/html/hi.txt web
container exec_die web
container export web
image tag myweb:snapshot
image import sha256:56e3f92534ca8c5aedd8de576ae2433f1864d3d3b51de847a8dc8dd7d503da4d
container create sharp_gould
container attach sharp_gould
network connect bridge
container start sharp_gould
network disconnect bridge
container die sharp_gould
container destroy sharp_gould
container prune <no value>
network prune <no value>
image untag sha256:0bf6577d7774e592b29d093e3ac9390eb91e13d72c21e506afce736de5e0eeb0
image delete sha256:0bf6577d7774e592b29d093e3ac9390eb91e13d72c21e506afce736de5e0eeb0
builder prune <no value>

$ docker events --since 10s --until 0s --filter type=container --format '{{.Action}} {{.Actor.Attributes.name}}'
prune <no value>
create evt
attach evt
start evt
die evt
destroy evt
  • 第一个示例从最近 30 秒内的事件开始流式输出,格式只输出事件类型、动作和名称;timeout 3 限制运行时间。
  • 第二个示例用 --filter type=container 限定容器事件,并通过 --since、--until 设置时间范围。
  • prune 事件的名称字段显示 <no value>,因为该事件没有此模板读取的名称值。

相关章节

动手练习

练习:只列出最近 10 秒内的容器事件,并输出事件动作和容器名称。

答案
docker events --since 10s --until 0s --filter type=container --format '{{.Action}} {{.Actor.Attributes.name}}'

常见问题

docker system df -v 中的 UNIQUE SIZE 是什么?

它列出镜像的独占大小;输出也会另外列出 SIZE 和 SHARED SIZE。例如,alpine:3.23 的 SIZE 是 13MB、SHARED SIZE 是 12.93MB、UNIQUE SIZE 是 86.86kB。

docker system prune 会删除正在使用的镜像吗?

示例中删除的是没有容器使用的悬空镜像,报告回收了 147.6MB;清理后仍有 3 个活动容器。--all 会将清理范围扩大到所有未使用的镜像,--volumes 则会清理匿名卷。

docker events 可以只查看容器事件吗?

可以,使用 --filter type=container。搭配 --since 和 --until 可限定事件时间范围,--format 可选择输出的事件字段。

如何只从 docker info 获取 Docker 版本和镜像数量?

用 docker info --format 指定 Go 模板字段;示例输出为 29.9.2 overlayfs 3 containers, 5 images。

更新于 2026年10月26日