清理未使用的 Docker 对象

Docker 对清理未使用的对象(通常称为“垃圾回收”),如镜像、容器、卷和网络,采取保守的方法。这些对象通常不会被删除,除非您明确要求 Docker 这样做。这可能导致 Docker 占用额外的磁盘空间。对于每种类型的对象,Docker 提供了一个 prune 命令。此外,您可以使用 docker system prune 来一次性清理多种类型的对象。本主题展示了如何使用这些 prune 命令。

清理镜像

docker image prune 命令允许您清理未使用的镜像。默认情况下,docker image prune 只清理悬空镜像。悬空镜像是指没有标签且未被任何容器引用的镜像。要删除悬空镜像:

$ docker image prune

WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y

要删除所有未被现有容器使用的镜像,请使用 -a 标志:

$ docker image prune -a

WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y

默认情况下,系统会提示您继续。要绕过提示,请使用 -f--force 标志。

您可以使用带有 --filter 标志的过滤表达式来限制哪些镜像被清理。例如,仅考虑超过24小时前创建的镜像:

$ docker image prune -a --filter "until=24h"

还有其他可用的过滤表达式。请参阅 docker image prune 参考 以获取更多示例。

清理容器

当你停止一个容器时,它不会自动被移除,除非你使用 --rm 标志启动它。要查看 Docker 主机上的所有容器,包括已停止的容器,请使用 docker ps -a。你可能会惊讶于存在多少容器,尤其是在开发系统上!已停止容器的可写层仍然占用磁盘空间。要清理这些内容,你可以使用 docker container prune 命令。

$ docker container prune

WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y

默认情况下,系统会提示您继续。要跳过提示,请使用 -f--force 标志。

默认情况下,所有已停止的容器都会被移除。您可以使用 --filter 标志来限制范围。例如,以下命令仅移除超过24小时的已停止容器:

$ docker container prune --filter "until=24h"

还有其他可用的过滤表达式。请参阅 docker container prune 参考 以获取更多示例。

清理卷

卷可以被一个或多个容器使用,并占用Docker主机的空间。卷永远不会被自动删除,因为这样做可能会破坏数据。

$ docker volume prune

WARNING! This will remove all volumes not used by at least one container.
Are you sure you want to continue? [y/N] y

默认情况下,系统会提示您继续。要绕过提示,请使用 -f--force 标志。

默认情况下,所有未使用的卷都会被删除。您可以使用 --filter 标志来限制范围。例如,以下命令仅删除未标记为 keep 标签的卷:

$ docker volume prune --filter "label!=keep"

还有其他可用的过滤表达式。请参阅 docker volume prune 参考 以获取更多示例。

修剪网络

Docker 网络不会占用太多磁盘空间,但它们确实会创建 iptables 规则、网桥网络设备和路由表条目。为了清理这些内容, 您可以使用 docker network prune 来清理未被任何 容器使用的网络。

$ docker network prune

WARNING! This will remove all networks not used by at least one container.
Are you sure you want to continue? [y/N] y

默认情况下,系统会提示您继续。要跳过提示,请使用 -f--force 标志。

默认情况下,所有未使用的网络都会被移除。您可以使用 --filter 标志来限制范围。例如,以下命令仅移除超过24小时的网络:

$ docker network prune --filter "until=24h"

还有其他可用的过滤表达式。请参阅 docker network prune 参考 以获取更多示例。

清理所有内容

docker system prune 命令是一个快捷方式,用于清理镜像、容器和网络。默认情况下不会清理卷,你必须指定 --volumes 标志给 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
        - unused build cache

Are you sure you want to continue? [y/N] y

要同时清理卷,请添加 --volumes 标志:

$ docker system prune --volumes

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all volumes not used by at least one container
        - all dangling images
        - all build cache

Are you sure you want to continue? [y/N] y

默认情况下,系统会提示您继续。要跳过提示,请使用 -f--force 标志。

默认情况下,所有未使用的容器、网络和镜像都会被移除。您可以使用 --filter 标志来限制范围。例如,以下命令会移除超过24小时的项目:

$ docker system prune --filter "until=24h"

还有其他可用的过滤表达式。请参阅 docker system prune 参考 以获取更多示例。