Show size of all docker volumes without mounting them
Asked Answered
L

4

10

How to list all docker volumes together with their respective size.

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.

Lesko answered 7/6, 2020 at 13:1 Comment(0)
A
10

Using the default overlay storage driver, a Docker volume doesn't have a "size" (all the volumes share space on the same filesystem). 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):

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. 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.

Alpenglow answered 7/6, 2020 at 13:59 Comment(0)
N
9

You can use the verbose output of docker system df. 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

I.e. simply run docker system df -v on the host machine and docker calculates conveniently the sizes for you.

Nicollenicolson answered 5/7, 2022 at 23:16 Comment(1)
Thanks. This response answers the question perfectly, better than the accepted answer.Undulant
B
3

I use this on Debian/Ubuntu:

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

The output looks like this:

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!

Bluegill answered 14/10, 2021 at 16:20 Comment(0)
S
3

du -h --max-depth=1 /var/lib/docker/volumes/

Steamtight answered 22/6, 2023 at 18:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.