我正在阅读 Docker 关于卷的用户指南部分:https://docs.docker.com/engine/userguide/containers/dockervolumes/
在第一部分,它指示如何创建卷并将其添加到容器,我将其称为“方法 1”:
You can use the -v multiple times to mount multiple data volumes. Now, mount a single volume in your web application container.
$ docker run -d -P --name web -v /webapp training/webapp python app.py
This will create a new volume inside a container at /webapp.
稍后会谈到数据卷容器,我将其称为“方法2”:
If you have some persistent data that you want to share between containers, or want to use from non-persistent containers, it’s best to create a named Data Volume Container, and then to mount the data from it.
$ docker create -v /dbdata --name dbstore training/postgres /bin/true
这是我不明白的,通过简单地使用-v
命令和docker run
(方法1)来实现卷与通过简单地使用-v
命令来实现卷有什么区别?使用“数据卷容器”实现卷(方法 2)?
这两种方式似乎都只是创建一个在图像的非持久容器的生命周期中持久存在的卷,如果我错了,请纠正我。
最佳答案
根据我的理解,至少从 docker 1.12 开始,卷始终是同一件事:存储在主机上某处的一些持久数据。但管理卷有不同的方法:
- 创建它并将其安装到容器中(您的方法 1)。这样,当您想要将一个容器的卷装载到另一个容器中时,您可以使用“volumes-from”
- 使用
dockervolumecreate
创建卷,并让docker管理它。它将保存在主机系统上的某个位置(例如/var/lib/docker),但您并不真正关心保存在何处以及如何保存。 - 将主机目录安装为卷,在这种情况下,您知道它安装在主机上的位置,并且您对其“负责”。
然后,还有一些“技巧”(如here所述。例如:
"if you create a named volume by running a new container from image by docker run -v my-precious-data:/data imageName, the data within the container under /data will be copied into the named volume."
因此,您创建/管理卷的方式可能会有所不同,但它本质上始终是一个卷。
关于Docker:创建数据卷容器与仅使用 -v 标志和 `run` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36743487/