Docker 官方文档提到我需要运行docker rm -v containername
来专门删除一个数据卷。但是如果你已经删除了所有引用特定数据卷的容器,你该怎么办?
答案1
在 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
答案2
在 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
答案3
清理命令:从 docker 1.13 开始引入清理命令。要删除所有未使用的容器、图像、网络和卷:
docker system prune
或单独:
docker container prune
docker image prune
docker network prune
docker volume prune
答案4
我有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()”函数声明添加到 root 的 .bashrc 文件中即可。该函数必须以 root 身份运行。