docker刪除沒用的鏡像


Docker采用保守的方法來清理未使用的對象(通常稱為“垃圾收集”),例如圖像,容器,卷和網絡:除非你明確地要求Docker這樣做,否則通常不會刪除這些對象。這可能導致Docker占用較多額外的磁盤空間。對於每種類型的對象,Docker都提供了一個prune命令修剪一次清理多種類型的對象。本文主要說明如何使用這些prune命令。
prune 鏡像
該Docker 鏡像 prune命令允許你清理未使用的鏡像。默認情況下,docker image prune僅清除dangling 鏡像。dangling images是未標記的鏡像,並且未被任何容器引用。要刪除dangling images:
要刪除現有容器未使用的所有鏡像,請使用-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 參考。
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 參考。
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
默認情況下,將刪除所有未使用的卷。您可以使用--filter標志限制范圍。例如,以下命令僅刪除未使用保留標簽標記的卷(下面的命令只刪除未標記為保留標簽的卷):
$ docker volume prune --filter"label!= keep"
其他過濾表達式可用。有關更多示例,請參閱docker volume prune 參考。
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小時的網絡:
prune 一切
docker system prune命令是修剪鏡像,容器,和網絡的快捷方式。在Docker 17.06.0及更早版本中,卷也被修剪。在Docker 17.06.1及更高版本中,必須為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 - all build cache Are you sure you want to continue? [y/N] y
如果您使用的是Docker 17.06.1或更高版本,並且還希望修剪卷,請添加--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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM