找出並移除不需要的 docker image,以及自動清理 docker 空間
Docker 用久了以後常常會有很多不需要的殘骸 image。
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-docker-image latest b52a8f66d908 5 minutes ago 240MB
<none> <none> 7e2397ff203a 6 minutes ago 240MB
<none> <none> ff90d79841b3 6 minutes ago 240MB
<none> <none> 7bf78317cc69 8 minutes ago 240MB
<none> <none> 468d6d409589 12 minutes ago 240MB
<none> <none> 4828834c44c8 12 minutes ago 240MB
nginx latest cd4e03b35a8e 5 days ago 134MB
clickhouse/clickhouse-server 22.3 fc50f9f05458 7 days ago 694MB
minio/minio latest dbde607de5d9 11 days ago 356MB
mysql 8.0.29-oracle 55ca64bef8c5 13 days ago 482MB
redis latest 54b9f01b5a59 2 weeks ago 111MB
php 8.1-fpm-alpine e475fd4508ec 2 weeks ago 66.8MB
python 3.8-slim-buster bff3f4a3c478 3 weeks ago 109MB
...
那些標記為 <none>
的項目就是之前 build 出來,但修改重 build 後就用不到的殘骸。
可以簡單用行指令快速移除這些 docker image:
$ docker images | grep none | awk '{print $3}' | xargs -I {} docker rmi {}
Deleted: sha256:7e2397ff203ae4a0f380caba25610bd62e02addb7a6ee7669890dbf1954b87fc
Deleted: sha256:ff90d79841b3140bbd45a293886d2b3089fa35ad82e6850e0c70792e1695bab7
Deleted: sha256:7bf78317cc694ae1fde193ff1c5b714ac02aa33f84ec171de0fe187ef2cb6d00
Deleted: sha256:468d6d4095899b880fa42ffd516cb599eee11967b2c208fd6fe2badf959fbe28
Deleted: sha256:4828834c44c826300fa780538f138b5c9ab5f0f48e293d8d852660bcdf1801f7
除此之外,docker 也提供一些系統工具可以使用。
例如 docker system df
可以看到空間使用報告。
$ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 23 0 14.11GB 14.11GB (100%)
Containers 0 0 0B 0B
Local Volumes 60 0 1.747GB 1.747GB (100%)
Build Cache 75 0 160.6MB 160.6MB
加上 -v
參數可以看到更完整的資料,資訊較多。
$ docker system df -v
Images space usage:
REPOSITORY TAG IMAGE ID CREATED SIZE SHARED SIZE UNIQUE SIZE CONTAINERS
my-docker-image latest b52a8f66d908 17 minutes ago 240.1MB 74.35MB 165.7MB 0
nginx latest cd4e03b35a8e 5 days ago 134.5MB 74.35MB 60.13MB 0
clickhouse/clickhouse-server 22.3 fc50f9f05458 7 days ago 693.8MB 65.64MB 628.2MB 0
minio/minio latest dbde607de5d9 11 days ago 356.3MB 0B 356.3MB 0
clickhouse/clickhouse-server 22.6 d430bed3cd30 11 days ago 729.3MB 65.64MB 663.7MB 0
mysql 8.0.29-oracle 55ca64bef8c5 13 days ago 481.5MB 0B 481.5MB 0
redis latest 54b9f01b5a59 2 weeks ago 111.2MB 0B 111.2MB 0
php 8.1-fpm-alpine e475fd4508ec 2 weeks ago 66.84MB 0B 66.84MB 0
python 3.8-slim-buster bff3f4a3c478 3 weeks ago 109.1MB 109.1MB 0B 0
(中略)
Containers space usage:
CONTAINER ID IMAGE COMMAND LOCAL VOLUMES SIZE CREATED STATUS NAMES
31aeba1940ac my-image-1 "/bin/sleep 999999999" 0 0B 7 seconds ago Up 3 seconds my-container-1
ee60b699262e my-image-2 "/bin/sleep 999999999" 0 0B 7 seconds ago Up 3 seconds my-container-2
bcdfa438ce80 my-image-3 "/bin/sleep 999999999" 0 0B 7 seconds ago Up 3 seconds my-container-3
1610dbd7be6e my-image-4 "/bin/sleep 999999999" 0 0B 7 seconds ago Up 3 seconds my-container-4
(中略)
Local Volumes space usage:
VOLUME NAME LINKS SIZE
a62f8f5478ad199a7d2b32e52df64ec397b956d94eda2b9ddc92ec53ae2894f2 0 1.448MB
db9a888d2c1e82705b163dbc29d9db3b3d761566b985c56e82b734c8749038a0 0 1.197MB
7a46ff272676689b73b7bdcb214be09726d32b57ad3d598a68a8f1bf1894a844 0 88B
30b742ec40f689cdd15798af9e3c82586e52ad1327e4921c48225db2fc142e0e 0 88B
6943289b61b319a40214b9258c9d6360ca1d74c38552de9a54cdbc3b45eb395f 0 11B
ae1a487ab0e366355aa9ce32118f8061e189af799584b61bb55dbb7a451cb769 0 88B
(中略)
Build cache usage: 160.6MB
CACHE ID CACHE TYPE SIZE CREATED LAST USED USAGE SHARED
jx11eyv5w9x7 regular 1.94kB 9 days ago 9 days ago 1 true
yxvtz0lehcn9 regular 1.45kB 9 days ago 9 days ago 1 true
bit5n61r9bsp regular 0B 9 days ago 9 days ago 1 true
j24gwespruyq regular 1.6kB 9 days ago 9 days ago 1 true
kwiex408x4bp regular 847B 9 days ago 9 days ago 1 true
ob9j0kghhtgb regular 0B 9 days ago 9 days ago 1 true
8h96kipuq14o regular 238B 9 days ago 9 days ago 1 true
(中略)
docker 甚至有提供 docker system prune
這樣的指令可以做全自動空間清理。但使用前要注意,所有關閉狀態的 container 都會被刪除,如果有 container 要保留還是手動清除比較保險。
$ docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all dangling build cache
Are you sure you want to continue? [y/N] y
Deleted Containers:
ad0983f71b815c6fb1061df23b8f1711ff4fa9a33c31b225f85314de42a1bfc5
e5e775c7c337d9034ca04648042d0e46e31ca3cbea1c1efd39cc6b6b58bdef67
be8a6d6a2ed4bdee9362983de1dbbdcafa78dce104d7029848ad0cd6304cde37
73a66bcb182c6466dfaced47e269bea547ada3c684cd6d65dc0805d0d2d11bb8
a199901902afa7232e87a18a9cce3b5d4c3b92bad97061b30c8e04cfe5808b2d
506d536f022ebf15e2b6d7360af7ccf4d95078e57b9bce6f7edf815d4911eaf7
4e7e011e2f04ab4b486b9bf2f7074e6ac3723a71286677ff399e77f00b977a46
Deleted Networks:
my-net_default
Deleted build cache objects:
ww9pknzsw7jb3tpaslwk13k0b
xb6xkmtfuqrw21ps3ctwga9lv
zrgrtyojc94i0t84tyyrgdddx
ubd23bw0a6v6kupykkqbij6k9
o7mf4yavabzyvqsxciww7mpra
oln52adhhsw9jzlsevt7c57vr
uou2nkzjeno90dnp9lbwgc6dy
771wljz9bl4p3jhc3qdawmseq
pneo8byr8ffhyl1uhr37n0eky
w6l140526dgl0y2omsx7rlk9i
i7przfq1nm3d0w847mi9cuft5
Total reclaimed space: 160.6MB