33

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?

Bonifacio2
  • 3,405
  • 6
  • 34
  • 54

6 Answers6

54

You can use:

docker system df -v

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

MikeWu
  • 3,042
  • 2
  • 19
  • 27
15

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)
Federkun
  • 36,084
  • 8
  • 78
  • 90
  • 5
    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`. – Bonifacio2 Aug 09 '19 at 13:25
  • Are you on ios, on docker-machine? – Federkun Aug 09 '19 at 13:31
  • 1
    I'm on MacOS, which is the machine running docker. – Bonifacio2 Aug 09 '19 at 13:33
  • 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. – Federkun Aug 09 '19 at 13:35
  • 1
    No, I'm not using a VM. My containers are running on my machine. – Bonifacio2 Aug 09 '19 at 23:31
  • 1
    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. – Bonifacio2 Aug 13 '19 at 11:56
  • nice, I didn't know until now how you could ssh into docker vm on ios, glad to have that answered as well – Federkun Aug 13 '19 at 12:37
  • On MacOS, I did this that worked: `docker run --rm -v /:/vm-root alpine du -sH /vm-root/$(docker volume inspect --format '{{ .Mountpoint }}' volumeNameHere)` – levsa Oct 16 '20 at 13:00
  • 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. – logoff Jun 29 '23 at 16:32
4

Building on Federkun's answer:

docker run --rm -v /:/vm-root alpine du -sH /vm-root/$(docker volume inspect --format '{{ .Mountpoint }}' volumeNameHere)
levsa
  • 930
  • 9
  • 16
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.

Bonifacio2
  • 3,405
  • 6
  • 34
  • 54
3

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.

FranzBranntwein
  • 101
  • 1
  • 7
0

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]]); }'
Yuval
  • 3,207
  • 32
  • 45