docker 系统修剪

描述删除未使用的数据
用法docker system prune [OPTIONS]

描述

删除所有未使用的容器、网络、图像(悬空和未使用的)以及可选的卷。

选项

选项默认描述
-a, --all删除所有未使用的图像,而不仅仅是悬空的图像
--filterAPI 1.28+ 提供过滤器值(例如label=<key>=<value>
-f, --force不提示确认
--volumes修剪匿名卷

例子

$ docker system prune

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all dangling images
        - unused build cache
Are you sure you want to continue? [y/N] y

Deleted Containers:
f44f9b81948b3919590d5f79a680d8378f1139b41952e219830a33027c80c867
792776e68ac9d75bce4092bc1b5cc17b779bc926ab04f4185aec9bf1c0d4641f

Deleted Networks:
network1
network2

Deleted Images:
untagged: hello-world@sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f
deleted: sha256:1815c82652c03bfd8644afda26fb184f2ed891d921b20a0703b46768f9755c57
deleted: sha256:45761469c965421a92a69cc50e92c01e0cfa94fe026cdd1233445ea00e96289a

Total reclaimed space: 1.84kB

默认情况下,如果当前没有容器使用该卷,则不会删除卷以防止重要数据被删除。--volumes 运行命令时也使用该标志来修剪匿名卷:

$ docker system prune -a --volumes

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all anonymous volumes 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 Containers:
0998aa37185a1a7036b0e12cf1ac1b6442dcfa30a5c9650a42ed5010046f195b
73958bfb884fa81fa4cc6baf61055667e940ea2357b4036acbbe25a60f442a4d

Deleted Networks:
my-network-a
my-network-b

Deleted Volumes:
named-vol

Deleted Images:
untagged: my-curl:latest
deleted: sha256:7d88582121f2a29031d92017754d62a0d1a215c97e8f0106c586546e7404447d
deleted: sha256:dd14a93d83593d4024152f85d7c63f76aaa4e73e228377ba1d130ef5149f4d8b
untagged: alpine:3.3
deleted: sha256:695f3d04125db3266d4ab7bbb3c6b23aa4293923e762aa2562c54f49a28f009f
untagged: alpine:latest
deleted: sha256:ee4603260daafe1a8c2f3b78fd760922918ab2441cbb2853ed5c439e59c52f96
deleted: sha256:9007f5987db353ec398a223bc5a135c5a9601798ba20a1abba537ea2f8ac765f
deleted: sha256:71fa90c8f04769c9721459d5aa0936db640b92c8c91c9b589b54abd412d120ab
deleted: sha256:bb1c3357b3c30ece26e6604aea7d2ec0ace4166ff34c3616701279c22444c0f3
untagged: my-jq:latest
deleted: sha256:6e66d724542af9bc4c4abf4a909791d7260b6d0110d8e220708b09e4ee1322e1
deleted: sha256:07b3fa89d4b17009eb3988dfc592c7d30ab3ba52d2007832dffcf6d40e3eda7f
deleted: sha256:3a88a5c81eb5c283e72db2dbc6d65cbfd8e80b6c89bb6e714cfaaa0eed99c548

Total reclaimed space: 13.5 MB

过滤(--filter)

过滤标志( --filter)格式为“key=value”。如果有多个过滤器,则传递多个标志(例如,--filter "foo=bar" --filter "bif=baz"

目前支持的过滤器有:

  • 直到 ( <timestamp>) - 只删除给定时间戳之前创建的容器、镜像和网络
  • label ( label=<key>label=<key>=<value>label!=<key>或) - 仅删除带有(或不带有,如果使用)指定标签的label!=<key>=<value>容器、图像、网络和卷。label!=...

过滤until器可以是 Unix 时间戳、日期格式的时间戳或相对于守护进程机器的时间计算的Go 持续时间字符串(例如10m, )。1h30m日期格式时间戳支持的格式包括 RFC3339Nano、RFC3339、2006-01-02T15:04:052006-01-02T15:04:05.9999999992006-01-02Z07:002006-01-02。如果您未在时间戳末尾提供时区偏移量,Z则将 使用守护程序上的本地时区。+-00:00提供 Unix 时间戳时,输入秒[.nanoseconds],其中秒是自 1970 年 1 月 1 日(UTC/GMT 午夜)以来经过的秒数,不包括闰秒(又名 Unix 纪元或 Unix 时间),以及可选的 .纳秒字段是秒的一小部分,长度不超过九位数字。

label过滤器接受两种格式。一种是label=...(label=<key>label=<key>=<value>),它删除具有指定标签的容器、映像、网络和卷。另一种格式是label!=...(label!=<key>label!=<key>=<value>),它会删除没有指定标签的容器、映像、网络和卷。