?
This document uses PHP Chinese website manual Release
Docker對未使用的對象%28(通常稱為“垃圾收集”%29)采取保守的清理方法,例如圖像、容器、卷和網(wǎng)絡(luò):除非明確要求Docker這樣做,否則通常不會刪除這些對象。這可能導(dǎo)致Docker使用額外的磁盤空間。對于每種類型的對象,Docker提供了一個prune
命令。此外,您還可以使用docker system prune
一次清除多種類型的對象。本主題演示如何使用這些prune
命令。
大docker image prune
命令允許您清理未使用的圖像。默認情況下,docker image prune
只清理懸吊圖像。懸空圖像是沒有標記的,也沒有被任何容器引用的圖像。若要刪除懸掛的圖像,請執(zhí)行以下操作:
$ docker image prune WARNING! This will remove all dangling images.Are you sure you want to continue? [y/N] y
若要刪除現(xiàn)有容器未使用的所有圖像,請使用-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
默認情況下,將提示您繼續(xù)。若要繞過提示符,請使用-f
或--force
旗子。
使用過濾表達式,可以限制哪些圖像被剪除。--filter
旗子。例如,僅考慮24小時前創(chuàng)建的圖像:
$ docker image prune -a --filter "until=24h"
還有其他過濾表達式可用。見docker image prune
參照系想要更多的例子。
停止容器時,除非使用--rm
旗子。若要查看Docker主機上的所有容器,包括已停止的容器,請使用docker ps -a
您可能會驚訝于有多少容器存在,特別是在開發(fā)系統(tǒng)%21 A中,停止容器的可寫層仍然占用磁盤空間。若要清除此問題,可以使用docker container prune
命令。
$ docker container prune WARNING! This will remove all stopped containers.Are you sure you want to continue? [y/N] y
默認情況下,將提示您繼續(xù)。若要繞過提示符,請使用-f
或--force
旗子。
默認情況下,所有已停止的容器都將被移除??梢允褂?code>--filter旗子。例如,下面的命令只刪除已停止的容器,這些容器的時間超過24小時:
$ docker container prune --filter "until=24h"
還有其他過濾表達式可用。見docker container prune
參照系想要更多的例子。
卷可以由一個或多個容器使用,并占用Docker主機上的空間。卷永遠不會自動刪除,因為這樣做會破壞數(shù)據(jù)。
$ 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
默認情況下,將提示您繼續(xù)。若要繞過提示符,請使用-f
或--force
旗子。
默認情況下,將刪除所有未使用的卷。可以使用--filter
旗子。例如,下面的命令只刪除未使用keep
標簽:
$ docker volume prune --filter "label!=keep"
還有其他過濾表達式可用。見docker volume prune
參照系想要更多的例子。
Docker網(wǎng)絡(luò)占用的磁盤空間不多,但它們確實創(chuàng)建了iptables
規(guī)則、橋接網(wǎng)絡(luò)設(shè)備和路由表條目。要清理這些東西,你可以用docker network prune
清理沒有被任何容器使用的網(wǎng)絡(luò)。
$ 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
默認情況下,將提示您繼續(xù)。若要繞過提示符,請使用-f
或--force
旗子。
默認情況下,所有未使用的網(wǎng)絡(luò)都將被刪除。可以使用--filter
旗子。例如,以下命令只刪除24小時以上的網(wǎng)絡(luò):
$ docker network prune --filter "until=24h"
還有其他過濾表達式可用。見docker network prune
參照系想要更多的例子。
大docker system prune
命令是修剪圖像、容器和網(wǎng)絡(luò)的快捷方式。在Docker 17.06.0和更早版本中,卷也會被修剪。在Docker 17.06.1及更高版本中,必須指定--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 - all build cache Are you sure you want to continue? [y/N] y
如果您使用的是Docker 17.06.1或更高版本,并且希望也修剪卷,請?zhí)砑?code>--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
默認情況下,將提示您繼續(xù)。若要繞過提示符,請使用-f
或--force
旗子。