Docker – Removing Docker data volumes

docker

The official Docker documentation mentions that I need to run docker rm -v containername to specifically remove a data volume. But what do you do if you already removed all the containers referencing the specific data volume ?

Best Answer

Before version 1.9, Docker didn't provide any way to remove dangling volumes.

If such volumes are taking too much disk space and you want to take matters into your own hands though, you can manually delete the volumes by first identifying the ones which are in use. You can run docker inspect -f '{{ .Volumes }}' containername to find the location in the file system of the volumes in use, and then delete everything except those. If you have lots of containers, you can run for x in $(docker ps -qa | sed '1d'); do docker inspect -f '{{ .Volumes }}' ${x}; done to loop through the containers and list the volumes.

Better yet, you can use the Python script here, the prerequisite is to install the python API client for Docker pip install docker-py