After having played with Docker for some time on my computer, I have started to gather quite some images on my hard disk... In his post, Brian Christner provided a very good comparison of different scripts that can help you to clean up some of the images that are not needed anymore.

From my previous Docker-related posts, you can see that one of the Docker features I am looking most at is the data volumes. Similar to the problem of a growing pile of images on my hard disk, I also had a growing pile of (un)named data volumes.

In order to get a bit of an idea how large this pile was, I started to investigate this. Instead of getting the information about the container/image sizes, I just want to have the size of the data volumes:

In the spirit of my motto to automate everything I can automate, I, of course, wrote a script to get me the information I needed :)

The trivial approach would be to just check the /var/lib/docker/volumes folder. However, I wanted the script to be able to deal with volume plugins. Furthermore, I also wanted to be able to get some additional information about each data volume.

The current version of the script is very naive and just loops over all data volumes, starts a container and mounts the data volume in the container in order to enable us to check the size of that volume in the container. Using the Docker API, I think better and more efficient alternatives are possible than this :)

After we have received the size of the data volume, we also get some additional information regarding the running and stopped containers that have a known link to this data volume. This information can help us determine which data volumes we must not delete ;)

To get the size of a data volume myDataVolume, I make use of the following one-liner:

docker_volume_size=$(docker run --rm -t -v myDataVolume:/volume_data ubuntu bash -c "du -hs /volume_data | cut -f1" )  

This will store the size of the data volume in the variable docker_volume_size. To obtain a list of all data volumes present, we can use the command docker volume ls -q. By combining the above one-liner with this command, I can loop over all of the data volumes and query its size.

Besides the size, I also would like to get information about the containers that are connected to this data volume. And for each container I would also like to have both the name of the image as well as the status (running or stopped).

If we have a known Docker data volume myDataVolume, you can get this information with the following one-liner:

docker ps -a --filter=volume=myDataVolume --format "{{.Names}} [{{.Image}}] ({{.Status}})"  

Using this Go template we extract the name of the container, as well as the image it was created from and the current status.

By combining all the above into one script, we can get detailed information about all the data volumes that are present on the system. An example output is shown in the image below:
Example output of data volume information script

I added the script to my Docker Convenience Scripts repository. In the current version, I just show everything to the screen. Some features for a future version could be a performance improvement and creating a structured output (e.g. JSON) instead of the current output. If you have ideas for this, you can fork my repository and send me a PR.