我有运行大量Docker容器的测试。每个都有一个音量。
如何知道需要删除的卷名?
例如:
~ docker run -d registry:2
~ docker volume inspect c80fc65a79039d70cf54b3af3ab66b378dec42d0757928ae94277b197d8d8104
[
{
"CreatedAt": "2020-08-14T11:33:50Z",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/c80fc65a79039d70cf54b3af3ab66b378dec42d0757928ae94277b197d8d8104/_data",
"Name": "c80fc65a79039d70cf54b3af3ab66b378dec42d0757928ae94277b197d8d8104",
"Options": null,
"Scope": "local"
}
]
手动停止并删除registry:2
容器后,该卷仍然存在。我不想删除所有卷,因为其中一些仍在使用中。
最佳答案
您无需自己确定卷名。实际上,您在这里有多个选项。
如果要清理,可以在--rm
上使用docker run
标志
引用:docs.docker.com: Clean Up (--rm)
If you set the --rm flag, Docker also removes the anonymous volumes associated with the container when the container is removed. This is similar to running docker rm -v my-container. Only volumes that are specified without a name are removed. For example, when running:
使用
docker system prune --volumes
命令清理至少一个容器未使用的所有卷引用:docs.docker.com: docker system prune
Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.
获取助理卷
如果您真的想获取容器的体积,可以使用代码段
docker inspect -f '{{ .Name }}{{ printf "\n" }}{{ range .Mounts }}{{ printf "\n\t" }}{{ .Type }} {{ if eq .Type "bind" }}{{ .Source }}{{ end }}{{ .Name }} => {{ .Destination }}{{ end }}{{ printf "\n" }}' <continaer-id>
关于docker - 如何删除(已删除)docker-container的docker-volume?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63412096/