1. 程式人生 > 其它 >docker檢視容器佔用磁碟大小

docker檢視容器佔用磁碟大小

Docker 1.13引入了docker system df命令,類似於Linux上的df命令,用於檢視Docker的磁碟使用情況。

$ docker system df
TYPE                TOTAL     ACTIVE    SIZE          RECLAIMABLE
Images              3         0         5.373GB       5.373GB (100%)
Containers          0         0         0B            0B
Local Volumes       0         0         0B            0B

檢視單個image、container大小:docker system df -v

[root@localhost ~]# docker system df -v
Images space usage:
 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE                SHARED SIZE         UNIQUE SIZE         CONTAINERS
mongo               latest              66c68b650ad4        
5 weeks ago 387.8MB 64.21MB 323.6MB 1 rabbitmq management 1c1e1f201079 7 weeks ago 181.5MB 151.4MB 30.09MB 1 rabbitmq latest 3e5ddca64bb7 7 weeks ago 151.4MB 151.4MB 0B 0 Containers space usage: CONTAINER ID IMAGE COMMAND LOCAL VOLUMES SIZE CREATED STATUS NAMES 1a0bb1ac1c52 mongo
"docker-entrypoint.s…" 1 2.74MB 5 weeks ago Up 7 hours docker_mongodb 73417264c538 rabbitmq:management "docker-entrypoint.s…" 1 1.73kB 6 weeks ago Exited (255) 2 days ago rabbitmq Local Volumes space usage: VOLUME NAME LINKS SIZE 13856e3cfd4af0a2d695cca746c4039213a66901c43326a3c14391c61bf07d21 0 285.6MB 2e7a18c2cde8bc82ad145138d495b3bdfe7329870a130874ef11aff57df2a955 1 0B 556df6317f798afb15cf6bdc8c6763ab45cabf2b09304545bcd319049ff61cef 1 338.9MB e7c82573f88c7e083445cbc611ed753ab26e36c73964fed323f7e38e3cb431b4 0 440.5MB Build cache usage: 0B CACHE ID CACHE TYPE SIZE CREATED LAST USED USAGE SHARED

Docker提供了docker system prune,可以用於清理dangling映象(參考What are Docker <none>:<none> images?)和容器,以及失效的資料卷和網路。

$ docker system prune
WARNING! This will remove:
    - all stopped containers
    - all volumes not used by at least one container
    - all networks not used by at least one container
    - all dangling images
Are you sure you want to continue? [y/N] y
Deleted Containers:
1cdf866157b4a97e151125af3c2a7f186a59b6f63807e2014ce1a00d68f44e1d
Deleted Images:
deleted: sha256:f59bb277...
deleted: sha256:695b8e70...
deleted: sha256:93b1cceb...
deleted: sha256:c74d6bcd...
deleted: sha256:df8b9bb1...
deleted: sha256:dfe8340f...
deleted: sha256:ce1ee654...
Total reclaimed space: 3.221GB

名稱為<none>:<none>,為dangling映象,因此會被刪除。同時,所有的中間映象也會被刪除。

更進一步,使用-a選項可以做深度清理。這時我們會看到更加嚴重的WARNING資訊:

$ docker system prune -a
WARNING! This will remove:
    - all stopped containers
    - all volumes not used by at least one container
    - all networks not used by at least one container
    - all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: test:latest
deleted: sha256:c515ebfa2...
deleted: sha256:07302c011...
deleted: sha256:37c0c6474...
deleted: sha256:5cc2b6bc4...
deleted: sha256:b283b9c35...
deleted: sha256:8a8b9bd8b...
untagged: alpine:latest
untagged: alpine@sha256:58e1a1bb75db1...
deleted: sha256:4a415e366...
deleted: sha256:23b9c7b43...
Total reclaimed space: 2.151GB

這個命令將清理整個系統,並且只會保留真正在使用的映象,容器,資料卷以及網路,因此需要格外謹慎。比如,我們不能在生產環境中執行prune -a命令,因為一些備用映象(用於備份,回滾等)有時候需要用到,如果這些映象被刪除了,則執行容器時需要重新下載。

此時,所有未繫結容器的映象將會被刪除。由於第一次prune命令刪除了所有容器,因此所有映象(它們沒有繫結任何容器)都會被刪除。

參考:

https://blog.csdn.net/dmw412724/article/details/107126828

http://dockone.io/article/2258