清理未使用的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 命令是一个快捷方式,用于清理镜像、容器和网络。默认情况下不会清理卷,您必须为 docker system prune 指定 --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

要同时删除卷,请添加 --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 参考