码头安装在主机上的卷 [英] docker mounting volumes on host
问题描述
我已成功地能够使用
docker run -v /host/path:/container/path ...
但是我的问题是这个和使用 VOLUME
在Dockerfile中的命令
But my question is what the difference between this and using the VOLUME
command in the Dockerfile
VOLUME /path
我使用的图像有一个 VOLUME
命令,我想知道如何与我的主机分享。我已经使用上面的 -v
命令完成了,但是我不知道我是否需要 -v
和 VOLUME
。
I am using an image that has a VOLUME
command, and I'd like to know how to share it with my host. I have done it using the -v
command above, but I didn't know if I needed both the -v
and VOLUME
.
推荐答案
VOLUME
命令将在容器内装载一个目录,并将容器文件结构之外的主机磁盘上的目录中创建或编辑的任何文件存储在绕过联合文件系统的位置。
The VOLUME
command will mount a directory inside your container and store any files created or edited inside that directory on your hosts disk outside the container file structure, bypassing the union file system.
这个想法是,您的数据库可以在您的Docker容器之间共享,只要有一个引用它们的容器(运行或停止),它们就会保持在周围。
The idea is that your volumes can be shared between your docker containers and they will stay around as long as there's a container (running or stopped) that references them.
当您运行时,您可以使用 - volumes从
命令,使其他容器装载现有卷(在容器之间有效共享)一个容器。
You can have other containers mount existing volumes (effectively sharing them between containers) by using the --volumes-from
command when you run a container.
VOLUME
和 -v $ c $之间的根本区别c>是这样的:
-v
将从您的Dock中的操作系统挂载现有文件呃容器和 VOLUME
将在您的主机上创建一个新的空卷,并将其装载到容器中。
The fundamental difference between VOLUME
and -v
is this: -v
will mount existing files from your operating system inside your docker container and VOLUME
will create a new, empty volume on your host and mount it inside your container.
示例:
- 您有一个Dockerfile定义了一个
VOLUME / var / lib / mysql
。 - 您构建停泊点图片并将其标记为
some-volume
- 您运行容器
然后,
- 您要使用此卷的另一个码头服务器映像
- 您使用以下命令运行docker容器:
docker run --volumes-from some-volume docker-image-name:tag
- 现在你有一个运行的docker容器,它的卷从
some-volume
载入/ var / lib / mysql
- You have another docker image that you want to use this volume
- You run the docker container with the following:
docker run --volumes-from some-volume docker-image-name:tag
- Now you have a docker container running that will have the volume from
some-volume
mounted in/var/lib/mysql
注意:使用 - 卷从
将会将卷挂载到卷的位置上的任何内容。即,如果你在 / var / lib / mysql
中有东西,它将被卷的内容所替代。
Note: Using --volumes-from
will mount the volume over whatever exists in the location of the volume. I.e., if you had stuff in /var/lib/mysql
, it will be replaced with the contents of the volume.
这篇关于码头安装在主机上的卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!