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 ?
In Docker 1.9, there is the ability to manage volumes better with docker volume
(see this PR):
$ docker --version
Docker version 1.9.0, build 76d6bc9
$ docker volume
Usage: docker volume [OPTIONS] [COMMAND]
Manage Docker volumes
Commands:
create Create a volume
inspect Return low-level information on a volume
ls List volumes
rm Remove a volume
Run 'docker volume COMMAND --help' for more information on a command
--help=false Print usage
$ docker volume ls -f dangling=true
DRIVER VOLUME NAME
local 0490a79a769b8fc96b901ad2b92be9f34516476be5d06da766b4fd8636275330
local source
local 89ecc5c7afc0c004b2eccff55692b842b4394ba12048195b385334ec7b161857
local 382a7ea81dd87f773914725d755ffe28110c3f07da5f135d6181bf55b393070a
local 0eef9492e7bf3f4e62fd1195b7284b32dd6f22ac8c99052f6ccf890c3d7a4e3c
local 499d1da8e35ebd51a2217c2ca68272740a6ae85a3d2e29e9abf54163d12f5c56
local da1cb72a3a39991b11992a8d5dbc3167771fb54b8eb41308657151ba934b981f
local 4530c88973639c6a4d6c35e9ee4a0ec48dcdd5767fa377214b4a32644ecb8947
local 784d941d6e1c7a89a064f9bbe3f594520174029efcbb792b3285915653801f0f
local 0250901593eecd6321a937fe321e5f88996716035ae51fa60952dcc1c5b8f884
local 92aa72c8a82facc0164f88b7216b2699dd4d393f7364ba5b8fd92e217c305d7e
local b88b70a407cd97cd5dfd0d93860ea620293abfe9090d2237cb88363846bfe8d3
local 5206cdf191052df750f6b37e37ce55455a03f6afcc6bfb03c46c8d1c87d5791f
local 88c510defbfc90a98026a88cca556725e60a15ac2ab579084664fa03b529670d
local d2538cf892e0f75f37e458e6240b7e31600f93d1fb40eb0190eba669f81e3b12
local e3d3cad4f814edd582b0dad2bfe069f9c69d9cc60b71e32a56690295269cac6e
local 120f36311a4f27497b7b89f22b1e0b7ace2c5e72f23cc1b869c895c37b4ed2db
local 42b8c7587137d493aac0388487ba745b077d27fb4f05f1d3f892246fef9f82f1
local 53dba9646ab87392c5c82768efb6b72a51de26564f0db2cacb790a3dccf846d2
These can be removed via this command:
$ docker volume rm $(docker volume ls -qf dangling=true)
42b8c7587137d493aac0388487ba745b077d27fb4f05f1d3f892246fef9f82f1
53dba9646ab87392c5c82768efb6b72a51de26564f0db2cacb790a3dccf846d2
5206cdf191052df750f6b37e37ce55455a03f6afcc6bfb03c46c8d1c87d5791f
88c510defbfc90a98026a88cca556725e60a15ac2ab579084664fa03b529670d
d2538cf892e0f75f37e458e6240b7e31600f93d1fb40eb0190eba669f81e3b12
e3d3cad4f814edd582b0dad2bfe069f9c69d9cc60b71e32a56690295269cac6e
120f36311a4f27497b7b89f22b1e0b7ace2c5e72f23cc1b869c895c37b4ed2db
0eef9492e7bf3f4e62fd1195b7284b32dd6f22ac8c99052f6ccf890c3d7a4e3c
0490a79a769b8fc96b901ad2b92be9f34516476be5d06da766b4fd8636275330
source
89ecc5c7afc0c004b2eccff55692b842b4394ba12048195b385334ec7b161857
382a7ea81dd87f773914725d755ffe28110c3f07da5f135d6181bf55b393070a
499d1da8e35ebd51a2217c2ca68272740a6ae85a3d2e29e9abf54163d12f5c56
da1cb72a3a39991b11992a8d5dbc3167771fb54b8eb41308657151ba934b981f
4530c88973639c6a4d6c35e9ee4a0ec48dcdd5767fa377214b4a32644ecb8947
784d941d6e1c7a89a064f9bbe3f594520174029efcbb792b3285915653801f0f
0250901593eecd6321a937fe321e5f88996716035ae51fa60952dcc1c5b8f884
92aa72c8a82facc0164f88b7216b2699dd4d393f7364ba5b8fd92e217c305d7e
b88b70a407cd97cd5dfd0d93860ea620293abfe9090d2237cb88363846bfe8d3
$ docker volume ls -f dangling=true
DRIVER VOLUME NAME
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
Clean up commands: as of docker 1.13 introduces clean-up commands. To remove all unused containers, images, networks and volumes:
docker system prune
or individually:
docker container prune
docker image prune
docker network prune
docker volume prune
Use command
docker volume ls -qf dangling=true | xargs -r docker volume rm
to clear dangling volumes from docker 1.9 and above.
I have docker 1.6.
The python version didn't work for me it was complaining docker version mismatch. It's amazing, that in times like this, nothing beats the good old way of doing things.
volpurge() {
if [ $(id -u) -ne 0 ];then
echo "please run as root"
return 1
fi
BASEDIR=/var/lib/docker/vfs/dir
a=$(mktemp)
b=$(mktemp)
c=$(mktemp)
printf "checking docker volumes ... "
cd $BASEDIR
ls -1 | sort -u > $a
cd -
tot1=$(wc -l < $a)
echo "found $tot1 total"
printf "inspecting volumes used by existing containers ... "
for x in $(docker ps -qa | sed '1d');do
docker inspect -f '{{ .Volumes }}' ${x} | tr ' ' '\n' | cut -d':' -f2 | tr -d ']' | grep $BASEDIR >> $b
done
sed -i 's/\/var\/lib\/docker\/vfs\/dir\///g' $b
sort -u $b > $c
mv -f $c $b
tot2=$(wc -l < $b)
echo "found $tot2 total"
if [ "$tot1" -gt "$tot2" ];then
comm -3 $a $b > $c
tot3=$(wc -l < $c)
echo "purging $tot3 dangling volumes ... "
read -p "proceed? [y/n]" decision
if [ "$decision" = "y" ];then
while read d;do
printf "."
rm -rf ${BASEDIR}/$d
done < $c
else
echo "OK. Please examine this manually"
cat $c
fi
else
echo "nothing to purge"
fi
rm -f $a $b $c
}
Simply add the 'volpurge()' function declaration into your root's .bashrc file. This function must be run as root.