Is there a way to determine the size of a docker volume on my macOS machine?
Asked Answered
P

6

58

I'm aware of docker volume ls and docker inspect -s, but the former doesn't show size info and the latter, even though it has a --size option, ignores it for volumes.

Is there something I'm missing?

Paw answered 9/8, 2019 at 13:5 Comment(0)
O
20

This:

docker volume inspect --format '{{ .Mountpoint }}' volumeNameHere

will return the mount point of the volume on your host. So, to get the size, it's just a matter of doing:

du -sh $(docker volume inspect --format '{{ .Mountpoint }}' volumeNameHere)
Osric answered 9/8, 2019 at 13:9 Comment(9)
This is weird. When I run docker volume inspect --format '{{ .Mountpoint }}' my-volume-id it returns /var/lib/docker/volumes/my-volume-id/_data, but du -sh $(docker volume inspect --format '{{ .Mountpoint }}' my-volume-id) returns No such file or directory.Paw
Are you on ios, on docker-machine?Osric
I'm on MacOS, which is the machine running docker.Paw
You are running docker in a VM then, which contains the volume in question. I'm not too sure about docker desktop, but it's just a matter of ssh into that vm and then run that command.Osric
No, I'm not using a VM. My containers are running on my machine.Paw
Thank you for your answer, Federkun. I figured out the details of ssh'ing into the virtual machine (I didn't even know it existed) and added them to another answer to this question.Paw
nice, I didn't know until now how you could ssh into docker vm on ios, glad to have that answered as wellOsric
On MacOS, I did this that worked: docker run --rm -v /:/vm-root alpine du -sH /vm-root/$(docker volume inspect --format '{{ .Mountpoint }}' volumeNameHere)Palmore
if you use Docker Desktop, you use a VM. so the files of the volume are inside the VM, not in the macOS host machine.Hardesty
W
89

You can use:

docker system df -v

More info at https://docs.docker.com/engine/reference/commandline/system_df/

Warner answered 15/9, 2020 at 19:47 Comment(0)
O
20

This:

docker volume inspect --format '{{ .Mountpoint }}' volumeNameHere

will return the mount point of the volume on your host. So, to get the size, it's just a matter of doing:

du -sh $(docker volume inspect --format '{{ .Mountpoint }}' volumeNameHere)
Osric answered 9/8, 2019 at 13:9 Comment(9)
This is weird. When I run docker volume inspect --format '{{ .Mountpoint }}' my-volume-id it returns /var/lib/docker/volumes/my-volume-id/_data, but du -sh $(docker volume inspect --format '{{ .Mountpoint }}' my-volume-id) returns No such file or directory.Paw
Are you on ios, on docker-machine?Osric
I'm on MacOS, which is the machine running docker.Paw
You are running docker in a VM then, which contains the volume in question. I'm not too sure about docker desktop, but it's just a matter of ssh into that vm and then run that command.Osric
No, I'm not using a VM. My containers are running on my machine.Paw
Thank you for your answer, Federkun. I figured out the details of ssh'ing into the virtual machine (I didn't even know it existed) and added them to another answer to this question.Paw
nice, I didn't know until now how you could ssh into docker vm on ios, glad to have that answered as wellOsric
On MacOS, I did this that worked: docker run --rm -v /:/vm-root alpine du -sH /vm-root/$(docker volume inspect --format '{{ .Mountpoint }}' volumeNameHere)Palmore
if you use Docker Desktop, you use a VM. so the files of the volume are inside the VM, not in the macOS host machine.Hardesty
A
9

As pointed out by the answer of @MikeWu, one can see the size of Docker Volumes with:

docker system df -v

but one has to scroll around a bit to find the section Local Volumes space usage:. Using a piped sed command like:

docker system df -v | sed -n '/VOLUME NAME/,/^ *$/p'

will help to improve the readability and reduce the output to the section of interest - by printing only from VOLUME NAME until first empty line.

Anastasius answered 27/1, 2022 at 12:21 Comment(1)
This is excellent and, instead of the accepted answer showing for a particular volume, this shows it for all volumes! Perfect for pre-pruning review.Matronly
P
4

Building on Federkun's answer:

docker run --rm -v /:/vm-root alpine du -sH /vm-root/$(docker volume inspect --format '{{ .Mountpoint }}' volumeNameHere)
Palmore answered 16/10, 2020 at 13:3 Comment(0)
P
3

The first step of Federkun's answer worked for me and it returned something like this:

/var/lib/docker/volumes/f4678f...b87/_data

But when I tried

du -sh $(docker volume inspect --format '{{ .Mountpoint }}' volumeNameHere)

it returned a

du: /var/lib/docker/volumes/f4678f...b87/_data: No such file or directory

It turns out Docker works differently on macOS. According to this answer, before I can run the du -sh command I must first screen into the docker virtual machine used by MacOS. You can do so by running

sudo screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty

and then you can run

du -sh /var/lib/docker/volumes/f4678f...

to finally get the volume size.

Paw answered 13/8, 2019 at 11:52 Comment(0)
V
3

You can use Go templates to format the DiskUsageContext:

docker system df --verbose --format '{{ range .Volumes }}{{ .Name }} {{ .Size }}\n{{ end }}'

And if you want to check for a specific volume, and convert the human-readable values back to bytes, you can use awk:

docker system df --verbose --format '{{ range .Volumes }}{{ .Name }} {{ .Size }}\n{{ end }}' | awk -v "volume_name=VOLUME_NAME"  'BEGIN { sizes["k"] = 1024; sizes["M"] = 1024^2; sizes["G"] = 1024^3; sizes[""] = 1; } match($0, "^" volume_name " ([0-9.]+)([kMG]?)B$", arr) { printf("%d\n", arr[1] * sizes[arr[2]]); }'
Variometer answered 25/7, 2023 at 20:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.