做者:張首富 時間:2020-07-19 w x:y18163201
咱們在以前的文章裏面講過Docker 垃圾回收機制裏面簡單的介紹了下docker * prune
命令,今天咱們來詳細的解讀下最後一個docker system prune
指令,解讀這個命令以前咱們先來了解下docker system df
html
此參數要在 client 和 service 端的版本:1.25 版本以上才能使用docker
這個命令是輸出 Docker 在宿主機上資源的使用狀況。能夠查看鏡像佔用空間大小,容器佔用空間大小,本地的volume 和 build 緩存的大小centos
建立模擬環境緩存
# docker pull centos:7.6.1810 # docker pull busybox # docker pull sellbot:20200617 # docker pull sellbot:20200614 # docker pull sellbot:20200613 # docker images REPOSITORY TAG IMAGE ID CREATED SIZE registry.cn-hangzhou.aliyuncs.com/smb/sellbot 20200716 f363f6d1f3f5 3 days ago 544MB registry.cn-hangzhou.aliyuncs.com/smb/sellbot 20200714 3b4aa15b779d 4 days ago 544MB registry.cn-hangzhou.aliyuncs.com/smb/sellbot 20200713 1f7133d6f068 6 days ago 544MB busybox latest c7c37e472d31 2 weeks ago 1.22MB centos 7.6.1810 f1cb7c7d58b7 16 months ago 202MB # docker run -id --name test centos:7.6.1810 /bin/bash -c "dd if=/dev/zero of=/tmp/1.txt bs=10000 count=2000 && sleep 300 " # docker run -id busybox /bin/sh -c "/bin/sleep 36000" # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b2fa31af4348 centos:7.6.1810 "/bin/bash -c 'dd if…" About a minute ago Up About a minute test 7bbaea0e2ded busybox "/bin/sh -c '/bin/sl…" 2 minutes ago Up 2 minutes clever_rubin
而後使用命令查看磁盤的使用狀況,bash
# docker system df TYPE TOTAL ACTIVE SIZE RECLAIMABLE Images 5 2 797.9MB 595MB (74%) Containers 5 1 20MB 20MB (100%) Local Volumes 0 0 0B 0B Build Cache 0 0 0B 0B
若是什麼參數不加這顯示數據的摘要信息,來解釋下每列的含義:網絡
1, Type: 顯示佔用磁盤空間的類型less
2, Total: 當前類型的總數量,好比在咱們這個環境一共有 5 個鏡像,ide
3, ACTIVE: 處於活動中的,當前環境中有兩個鏡像正在使用,ui
4, SIZE:當前鏡像,容器佔用磁盤的大小,spa
5,RECLAIMABLE:可回收,能夠被釋放的,百分比就是 RECLAIMABLE/SIZE 的大小。
咱們能夠查看下每一項的詳細信息
# docker system df -v Images space usage: REPOSITORY TAG IMAGE ID CREATED SIZE SHARED SIZE UNIQUE SIZE CONTAINERS registry.cn-hangzhou.aliyuncs.com/smb/sellbot 20200716 f363f6d1f3f5 3 days ago 543.9MB 518.4MB 25.51MB 0 registry.cn-hangzhou.aliyuncs.com/smb/sellbot 20200714 3b4aa15b779d 4 days ago 543.9MB 518.4MB 25.51MB 0 registry.cn-hangzhou.aliyuncs.com/smb/sellbot 20200713 1f7133d6f068 6 days ago 543.9MB 518.4MB 25.51MB 0 busybox latest c7c37e472d31 2 weeks ago 1.224MB 0B 1.224MB 4 centos 7.6.1810 f1cb7c7d58b7 16 months ago 201.8MB 0B 201.8MB 1 Containers space usage: CONTAINER ID IMAGE COMMAND LOCAL VOLUMES SIZE CREATED STATUS NAMES b2fa31af4348 centos:7.6.1810 "/bin/bash -c 'dd if…" 0 20MB 12 minutes ago Exited (0) 7 minutes ago test 7bbaea0e2ded busybox "/bin/sh -c '/bin/sl…" 0 0B 12 minutes ago Up 12 minutes clever_rubin ed33045b3f09 busybox "/bin/sh -c /bin/sle…" 0 0B 13 minutes ago Created zen_turing fba6d39f9b6a busybox "/bin/sh -c /bin/sle…" 0 0B 13 minutes ago Exited (1) 13 minutes ago pensive_lederberg 6c2e7a0b4e7a busybox "sh" 0 18B 14 minutes ago Exited (0) 14 minutes ago priceless_mirzakhani Local Volumes space usage: VOLUME NAME LINKS SIZE Build cache usage: 0B CACHE ID CACHE TYPE SIZE CREATED LAST USED USAGE SHARED
咱們能夠看到當咱們添加了-v
選項以後就能看到更多的信息了,相信喜歡觀察的您必定發現了在 images 這一列有一個有意思的現象,就是 SIZE = SHARED SIZE + UNIQUE SIZE
咱們來分開介紹每個表格裏面每一列的含義
Images space usage::
Containers space usage:
Local Volumes space usage:
Build cache usage: 0B
在咱們這個例子裏面只有 images 和 containers 有大小,是由於咱們沒有 build 過任何鏡像,也沒有把容器的時候掛載數據卷
注意:
這個數據卷並非-v 指定的數據卷,而是咱們在構建 Dockerfile 的時候寫的 VOLUME
咱們下面來作個實驗,把 VOLUME 弄出來
FROM centos:7.6.1810 MAINTAINER zhangshoufu(wx:y18163201) VOLUME /test/data
構建 Docker 鏡像
# docker build . -t centos:test_volume -f Dockerfile # docker images | grep volume centos test_volume 4c3708d6ffdd 16 seconds ago 202MB
啓動
# docker run -id --name test-volume-size -v /tmp/test:/test centos:test_volume /bin/bash -c "dd if=/dev/zero of=/test/data/1.txt bs=10000 count=2000 && sleep 300 "
查看
# docker system df -v | grep -A 3 "VOLUME NAME" VOLUME NAME LINKS SIZE c72d1b4b1e06d564e6d2ecb19de0daddbfbfa322a1b7545b5b1f8b418a2ecb46 1 20MB
到此咱們就給 Docker system df 命令講解完成了,知道這些信息,咱們自定義監控的時候就能獲取到一些特定的值,
它能刪除全部未使用的容器,網絡,鏡像(懸空和未引用),以及卷
不加參數使用它看看是什麼效果
# 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: 579f5e4e70638daa21e0b781bf3c394798df9bd70ceff0ba970d574961c74a4c f9725e158430855bf5e77ec04b66f6edb7eb59c24bd122457ff7a167381e7599 b2fa31af4348519fa4cdd8270a08498b6c771ece51bc14b3caccacdfd3ad671b ed33045b3f09eb3c245e1d49214420a8cd0e1a594a85c2323112a77d6006590c fba6d39f9b6a022472bd9c0083972dcb220a946924be8727a94b89f1a419872c 6c2e7a0b4e7a0517970c465b4b53af8b630fe3255ddab11fa1fb2144cb540d3f Total reclaimed space: 20MB
而後咱們 docker system df -v 看下結果
# docker system df -v Images space usage: REPOSITORY TAG IMAGE ID CREATED SIZE SHARED SIZE UNIQUE SIZE CONTAINERS centos test_volume 4c3708d6ffdd 9 minutes ago 201.8MB 201.8MB 0B 0 registry.cn-hangzhou.aliyuncs.com/smb/sellbot 20200716 f363f6d1f3f5 3 days ago 543.9MB 518.4MB 25.51MB 0 registry.cn-hangzhou.aliyuncs.com/smb/sellbot 20200714 3b4aa15b779d 5 days ago 543.9MB 518.4MB 25.51MB 0 registry.cn-hangzhou.aliyuncs.com/smb/sellbot 20200713 1f7133d6f068 6 days ago 543.9MB 518.4MB 25.51MB 0 busybox latest c7c37e472d31 2 weeks ago 1.224MB 0B 1.224MB 1 centos 7.6.1810 f1cb7c7d58b7 16 months ago 201.8MB 201.8MB 0B 0 Containers space usage: CONTAINER ID IMAGE COMMAND LOCAL VOLUMES SIZE CREATED STATUS NAMES 7bbaea0e2ded busybox "/bin/sh -c '/bin/sl…" 0 0B 3 hours ago Up 3 hours clever_rubin Local Volumes space usage: VOLUME NAME LINKS SIZE c72d1b4b1e06d564e6d2ecb19de0daddbfbfa322a1b7545b5b1f8b418a2ecb46 0 20MB Build cache usage: 0B CACHE ID CACHE TYPE SIZE CREATED LAST USED USAGE SHARED
從上面能夠看出來他給咱們失敗的 docker,或者說中止的容器所有都清除了,釋放了容器可寫層的大小,鏡像並未刪除,
注意:
docker system prune 默認只會刪除失敗的或者中止的容器,和他們的可層,也會刪除懸空的鏡像,並不會清除全部鏡像。
咱們可使用 --filter 參數清除哪一類容器,當前支持的參數
The currently supported filters are:
- until (
<timestamp>
) - only remove containers, images, and networks created before given timestamp- label (
label=<key>
,label=<key>=<value>
,label!=<key>
, orlabel!=<key>=<value>
) - only remove containers, images, networks, and volumes with (or without, in caselabel!=...
is used) the specified labels.
咱們可使用-a 參數清除當前機器上沒有被引用的鏡像
# docker system prune -a WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all images without at least one container associated to them - all build cache Are you sure you want to continue? [y/N] y Deleted Images: untagged: registry.cn-hangzhou.aliyuncs.com/smb/sellbot:20200714 untagged: registry.cn-hangzhou.aliyuncs.com/smb/sellbot@sha256:5a384898e7d11d99cefc55d9cd63518233b9751a5c4c465526cb14fff93985fd deleted: sha256:3b4aa15b779d469887b8477b53d7cac5f4e8373ab39d0de015dec4c952ed8f87 deleted: sha256:6619936848bb2c35b219c1b2726c09fa6e8e54cb51449b6654ceeb45460236f7 untagged: registry.cn-hangzhou.aliyuncs.com/smb/sellbot:20200713 untagged: registry.cn-hangzhou.aliyuncs.com/smb/sellbot@sha256:f56d67fb69575ac79b48eb8f7a1f112966d01b6e80c1e89adaa454c8c686ca3d deleted: sha256:1f7133d6f068499f91366eef403ed29478a9292720a3d59a8eb2f481560121aa deleted: sha256:28044d3747ae44664b37ca14d99afe450f73e2ef05b960279f821b231a5cca6b untagged: centos:test_volume deleted: sha256:4c3708d6ffdd898ec25728870ad3c97bd27cbaaf9637b1b45a4e85e5a92095a9 deleted: sha256:a9d487236d0a89428a695de3a1c340c5e740bf213a35dcd9a8053025733531cc untagged: registry.cn-hangzhou.aliyuncs.com/smb/sellbot:20200716 untagged: registry.cn-hangzhou.aliyuncs.com/smb/sellbot@sha256:b723f501cdebcee79b4d6b5b0859ce20718eded417928fa18d9f3b0135bc3ee4 deleted: sha256:f363f6d1f3f5fd25cedc0824fb33d17b5109d96a8a14a7931c7da8de812c37f6 deleted: sha256:16f95c955f85d8e1d5e6b3810e4843b9828bb0f5e215817661648b6d9c5a75b3 deleted: sha256:935e843cce2d562611a950de31beb63e17a9a1f3c3421ae3cab8db7f7ce3e1e0 deleted: sha256:bcf75aa1d1da5a81575b499af40a578888a8a7e0f813e0f516d7aefebc13690b deleted: sha256:9ad3e1811704d30494e2d402d432c00d1d9153784a9b31cc058fffc672557606 deleted: sha256:a3473da06c6e367bb9ecb640ecac13d916568324a15ae7e8fed8f465e0690464 deleted: sha256:4a7021bd2ec6699a4c2a3c7df7ef9197b678ad56193b5b1d309a10d89532666d deleted: sha256:6c3b476844a247d18c7eb535b9cc9d3d9f6d5063691cac5411f254b9fd7798f2 deleted: sha256:734e667e23a00543dbc5617963cb1473651316c0b1c0ba412e472960318d6e04 deleted: sha256:ffc9b21953f4cd7956cdf532a5db04ff0a2daa7475ad796f1bad58cfbaf77a07 untagged: centos:7.6.1810 untagged: centos@sha256:62d9e1c2daa91166139b51577fe4f4f6b4cc41a3a2c7fc36bd895e2a17a3e4e6 deleted: sha256:f1cb7c7d58b73eac859c395882eec49d50651244e342cd6c68a5c7809785f427 deleted: sha256:89169d87dbe2b72ba42bfbb3579c957322baca28e03a1e558076542a1c1b2b4a Total reclaimed space: 796.7MB [root@zhangsf docker]# docker system df TYPE TOTAL ACTIVE SIZE RECLAIMABLE Images 1 1 1.224MB 0B (0%) Containers 1 1 0B 0B Local Volumes 1 0 20MB 20MB (100%) Build Cache 0 0 0B 0B
可是並不會清除 VOLUME,若是咱們想清除 VOLUME,可使用下面參數,可是不建議使用
# docker system prune -f --volumes Deleted Volumes: c72d1b4b1e06d564e6d2ecb19de0daddbfbfa322a1b7545b5b1f8b418a2ecb46 Total reclaimed space: 20MB