清理未使用的 Docker 物件

Docker 在清理未使用物件(通常稱為“垃圾回收”)方面採取保守的方法,例如映象、容器、卷和網路。除非您明確要求 Docker 這樣做,否則這些物件通常不會被刪除。這可能導致 Docker 佔用額外的磁碟空間。對於每種型別的物件,Docker 都提供了一個 prune 命令。此外,您可以使用 docker system prune 一次性清理多種型別的物件。本主題將展示如何使用這些 prune 命令。

清理映象

docker image prune 命令允許您清理未使用的映象。預設情況下,docker image prune 只會清理懸空 (dangling) 映象。懸空映象指的是沒有標籤且未被任何容器引用的映象。要刪除懸空映象:

$ 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 參考文件