修剪未使用的 Docker 物件
Docker 對於清理未使用的物件(通常稱為「垃圾回收」)採取保守的作法,這些物件包含映像檔、容器、儲存卷和網路。除非您明確要求 Docker 執行清理,否則通常不會移除這些物件,這可能導致 Docker 佔用額外的磁碟空間。針對每一種類型的物件,Docker 都提供了 prune 指令。此外,您也可以使用 docker system prune 一次清理多種類型的物件。本主題將介紹如何使用這些 prune 指令。
清理映像檔
docker image prune 指令可讓您清理未使用的映像檔。預設情況下,docker image prune 只會清理「懸空」(dangling) 的映像檔。懸空映像檔是指沒有標籤 (tag) 且未被任何容器參照的映像檔。若要移除懸空映像檔:
$ 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 參考文件以取得更多範例。
清理儲存卷
儲存卷 (Volumes) 可由一個或多個容器使用,並會佔用 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 參考文件以取得更多範例。