如何移除 docker-volume of (removed) docker-container?
How to remove docker-volume of (removed) docker-container?
我有运行大量 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
容器后,卷仍然存在。
我不想删除所有卷,因为其中一些卷仍在使用中。
您可以手动删除未使用的卷:
docker volume rm c80fc65a79039d70cf54b3af3ab66b378dec42d0757928ae94277b197d8d8104
或prune所有未使用的卷
docker volume prune
删除所有未使用的本地卷。未使用的本地卷是那些未被任何容器引用的本地卷
您不需要自己确定卷名。其实,这里有多种选择。
如果你想清理
,你可以在docker run
上使用--rm
标志
参考: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 volume ls | awk '{if (length()==64 && !~/-/ && !~/_/) print }' | xargs -L1 docker volume rm
如果您想知道为什么存在这些随机卷,请阅读更多内容
这个 SO 答案可能对任何人都有帮助:
删除与容器关联的卷:。
我有运行大量 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
容器后,卷仍然存在。
我不想删除所有卷,因为其中一些卷仍在使用中。
您可以手动删除未使用的卷:
docker volume rm c80fc65a79039d70cf54b3af3ab66b378dec42d0757928ae94277b197d8d8104
或prune所有未使用的卷
docker volume prune
删除所有未使用的本地卷。未使用的本地卷是那些未被任何容器引用的本地卷
您不需要自己确定卷名。其实,这里有多种选择。
如果你想清理
,你可以在docker run
上使用--rm
标志
参考: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 volume ls | awk '{if (length()==64 && !~/-/ && !~/_/) print }' | xargs -L1 docker volume rm
如果您想知道为什么存在这些随机卷,请阅读更多内容
这个 SO 答案可能对任何人都有帮助:
删除与容器关联的卷: