Dockerfile中的Docker卷和VOLUME [英] docker volume and VOLUME inside Dockerfile
问题描述
我对创建 docker卷创建my-vol
和 VOLUME [ / var / www]之间的区别感到困惑
。
我的理解是:
1) docker volume create my-vol
在我们的计算机上创建一个持久卷,每个容器都可以链接到 my-vol
。
1) docker volume create my-vol
creates a persistent volume on our machine and each container could be linked to my-vol
.
2) VOLUME [ / var / www]
在其容器内创建一个卷。
2) VOLUME ["/var/www"]
creates a volume inside its container.
当我创建另一个容器时,可以如下链接
my-vol
:运行容器时的
And when I create another container, I could link
my-vol
as follows: when running a container
$ docker run -d --name devtest --mount source=myvol2,target=/app nginx:latest
当时,如果我添加 VOLUME [ / var / www]
在我的Dockerfile中,此docker文件的所有数据都将存储在 myvol2
和 / var / www
中吗?
At that time, if I added VOLUME ["/var/www"]
in my Dockerfile, all data of this docker file will be stored in both myvol2
and /var/www
?
推荐答案
Dockerfile VOLUME
c ommand说两件事:
The Dockerfile VOLUME
command says two things:
-
如果操作员未在特定容器目录上显式装载卷,请创建一个匿名目录
If the operator doesn't explicitly mount a volume on the specific container directory, create an anonymous one there anyways.
没有Dockerfile步骤将无法对该目录树进行进一步更改。
No Dockerfile step will ever be able to make further changes to that directory tree.
作为操作员,您可以使用 docker run -v 将卷(命名卷或主机目录)装入容器。 code>选项。您可以将其安装在容器中的任何目录上,而不管在Dockerfile中是否声明了
VOLUME
。
As an operator, you can mount a volume (either a named volume or a host directory) into a container with the docker run -v
option. You can mount it over any directory in the container, regardless of whether or not there was a VOLUME
declared for it in the Dockerfile.
(由于您可以使用 docker run -v
,而不管是否声明 VOLUME
,并且它具有令人困惑的副作用,我通常会避免在Dockerfile中声明 VOLUME
。)
(Since you can use docker run -v
regardless of whether or not you declare a VOLUME
, and it has confusing side effects, I would generally avoid declaring VOLUME
in Dockerfiles.)
就像在普通Linux中一样,在任何给定目录上只能(有用)安装一件事。使用您描述的设置,数据将存储在您创建并安装的 myvol2
中,并且在 / var / www $中可见c $ c>放在容器中,但是数据实际上只会存储在一个地方。如果删除并重新创建没有卷挂载的容器,则数据将不再存在。
Just like in ordinary Linux, only one thing can be (usefully) mounted on any given directory. With the setup you describe, data will be stored in the myvol2
you create and mount, and it will be visible in /var/www
in the container, but the data will only actually be stored in one place. If you deleted and recreated the container without the volume mount the data would not be there any more.
这篇关于Dockerfile中的Docker卷和VOLUME的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!