Answers:
在1.9版之前,Docker没有提供任何方法来删除悬空的卷。
如果此类卷占用了过多的磁盘空间,并且您想自己处理问题,则可以通过先确定正在使用的卷来手动删除这些卷。您可以运行docker inspect -f '{{ .Volumes }}' containername
查找正在使用的卷在文件系统中的位置,然后删除除那些之外的所有内容。如果您有很多容器,则可以运行for x in $(docker ps -qa | sed '1d'); do docker inspect -f '{{ .Volumes }}' ${x}; done
以遍历这些容器并列出卷。
更好的是,您可以在此处使用Python脚本,前提条件是为Docker安装python API客户端pip install docker-py
| sed '1d'
?它丢弃了第一个结果。中的-q
选项docker ps -qa
已经摆脱了输出中的标题行。我正在使用docker版本1.13,必须使用{{ .Mounts }}
代替{{ .Volumes }}
。
在Docker 1.9中,可以使用更好地管理卷docker volume
(请参阅此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
这些可以通过以下命令删除:
$ 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
dangling=false
列出的卷对我列出的卷完全相同,只是顺序不同。Docker 1.9.1版。听起来很腥;那个悬空的过滤器真的有用吗?啊,这是一个错误。github.com/docker/docker/issues/16095(标记为已关闭,但对我来说仍然是个问题...)
我有docker 1.6。
python版本对我不起作用,它抱怨docker版本不匹配。令人惊讶的是,在这样的时代,没有什么能比做旧的好方法更好了。
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
}
只需将'volpurge()'函数声明添加到根目录的.bashrc文件中即可。此功能必须以root用户身份运行。