简体   繁体   English

显示所有 docker 卷的大小而不安装它们

[英]Show size of all docker volumes without mounting them

How to list all docker volumes together with their respective size.如何列出所有 docker 卷及其各自的大小。

docker volume inspect $(docker volume ls)

gives information about all volumes but each volume's size.提供有关所有卷的信息,但每个卷的大小。

Preferably, I'd like to inspect the size without having to mount them into some container, as I have > 20 volumes with some cryptic "hash"-volume names, probably being produced to some error, which I'm trying to find.最好,我想检查大小而不必将它们安装到某个容器中,因为我有 > 20 个带有一些神秘的“散列”卷名称的卷,可能会产生一些错误,我正在尝试查找。

Using the default overlay storage driver, a Docker volume doesn't have a "size" (all the volumes share space on the same filesystem).使用默认overlay存储驱动程序,Docker 卷没有“大小”(所有卷共享同一文件系统上的空间)。 The only way to figure out the space consumed by the volume is by iterating over all the files in it and adding up their individual sizes.计算卷占用空间的唯一方法是遍历其中的所有文件并将它们各自的大小相加。 There are a couple ways to do this.有几种方法可以做到这一点。

I think the easiest is just to mount the volumes.我认为最简单的就是安装卷。 For example:例如:

docker volume ls -q |
while read vol; do 
  echo "=== $vol ==="
  docker run --rm -v "$vol:/data" alpine du -sh /data
done

If you really want to avoid docker run , you could do something like this (note that we're using jq to parse the JSON output from docker volume inspect ): If you really want to avoid docker run , you could do something like this (note that we're using jq to parse the JSON output from docker volume inspect ):

docker volume ls -q |
while read vol; do
  mount=$(docker volume inspect "$vol" | jq -r '.[0].Mountpoint')
  echo "=== $vol ==="
  sudo du -sh "$mount"
done

I prefer the first solution, because it will work even if the docker daemon is running on a remote host or virtual machine.我更喜欢第一个解决方案,因为即使 docker 守护程序在远程主机或虚拟机上运行,它也可以工作。 For example, the above will fail on either Windows or MacOS because on those systems, Docker is running a vm, so the volume paths won't be visible on the host when running du .例如,上述操作在 Windows 或 MacOS 上都将失败,因为在这些系统上,Docker 正在运行一个 vm,因此在运行du时卷路径在主机上不可见。

I use this on Debian/Ubuntu:我在 Debian/Ubuntu 上使用它:

sudo du -h /var/lib/docker/volumes | grep -iP '/var/lib/docker/volumes/[^/]+$'

The output looks like this: output 看起来像这样:

4.0K    /var/lib/docker/volumes/22c886640f89e83d0f807c52a839a2a28ad38fd3acbf7aaac38f5f62d4577ff6
0       /var/lib/docker/volumes/863a598091f9c70b1a7e66119a976df8786e9ade798d1acd6a606a66bb3e54c4
325M    /var/lib/docker/volumes/b4a9996e26b078b112583947053ac81799de3d3509d865dc5683490739803037
30M     /var/lib/docker/volumes/948eb93d038f78925831562cd4954e1d98ca53717586210a84070492d243b394
30M     /var/lib/docker/volumes/dd5290f49376a913d5d9b5c845353d0363134d133b6f25b0db667bf68e1822f3
35M     /var/lib/docker/volumes/b2e8426cbbf1c5278ccb0e3bb45dc614c11fef8699c502f25da1718906513487
317M    /var/lib/docker/volumes/f9dfa3f3189084c3a10c86d9dd874fbb4fd8cf059db4479b784e959d7513ca0e
0       /var/lib/docker/volumes/0b33f67db05002285e4e1778f29954130499a63ca4ed1070108187d999951aca
120K    /var/lib/docker/volumes/92c1297c42c7bced027014da5a2a24e365f64fd2cd109e92265a4fca4ea83e51
30M     /var/lib/docker/volumes/6060e487abbe52de6e08cf049c8b55f9456e0158050b4543073dc0d531395137
30M     /var/lib/docker/volumes/6c3f6e1798d3b640078c1b2bd15c39de3fc8004c58f5a1655b69d7c2024f6eb1
35M     /var/lib/docker/volumes/5fe0fce0dedc065f5f21bdea264ffde173a714d4eb77373d351429dffc0f42c4

Clearly not all volumes take up space but a good number of them do and at the time of writing this I found them using over 40 GiB!显然,并非所有卷都占用空间,但其中很多卷都占用空间,在撰写本文时,我发现它们使用了超过 40 GiB!

You can use the verbose output of docker system df .您可以使用docker system df的详细 output 。 This gives you detailed information on all space usages, also the volumes.这为您提供了有关所有空间使用情况以及卷的详细信息。

$ docker system df --help

Usage:  docker system df [OPTIONS]

Show docker disk usage

Options:
      --format string   Pretty-print images using a Go template
  -v, --verbose         Show detailed information on space usage

Ie simply run docker system df -v on the host machine and docker calculates conveniently the sizes for you.即只需在主机上运行docker system df -v , docker 即可方便地为您计算尺寸。

I believe the simplest option would be:我相信最简单的选择是:

docker system df -v

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM