将一个卷添加到Docker,但不包括一个子文件夹 [英] Add a volume to Docker, but exclude a sub-folder
问题描述
/ hostFolder
。现在,如果我想将这个文件夹添加到Docker容器中,那么我可以使用 Dockerfile $中的 ADD
c $ c>或将其作为一个音量加载。
到目前为止,很好。
现在 / hostFolder
包含一个子文件夹 / hostFolder / subFolder
。
我想将 / hostFolder
挂载到Docker容器中(无论是读写还是只读都无关紧要,对我而言),但是我坚持> NOT 想要包含 / hostFolder / subFolder
。我想排除这个,我也希望Docker容器能够对这个子文件夹进行更改,而不会在主机上发生更改。
<这可能吗?如果是,那么怎么样?解决方案首先,使用 ADD
不同于使用卷的Dockerfile(或通过 -v
参数到 docker run $ c $ c>或者一个Docker文件中的 VOLUME
指令)。 ADD
和 COPY
命令只需要在 docker build
运行。在使用 docker build
命令创建新图像之前,不会更新这些文件。相比之下,使用卷本质上是说这个目录不应该存储在容器映像中;而是使用主机上的目录;每当一个卷中的一个文件被更改时,主机和容器都将立即看到。
我不相信你可以使用卷实现你想要的,
但是,使用 COPY
(应优先选择 ADD
)。您可以使用 .dockerignore
文件排除子目录,或者您可以 COPY
所有文件然后执行运行rm bla
删除子目录。
请记住,使用添加到图像中的任何文件COPY
或 ADD
必须在构建环境内,即在您运行的目录中或之下 docker build
从。 Supposed I have a Docker container and a folder on my host /hostFolder
. Now if I want to add this folder to the Docker container as a volume, then I can do this either by using ADD
in the Dockerfile
or mounting it as a volume.
So far, so good.
Now /hostFolder
contains a sub-folder, /hostFolder/subFolder
.
I want to mount /hostFolder
into the Docker container (whether as read-write or read-only does not matter, works both for me), but I do NOT want to have it included /hostFolder/subFolder
. I want to exclude this, and I also want the Docker container be able to make changes to this sub-folder, without the consequence of having it changed on the host as well.
Is this possible? If so, how?
First, using the ADD
instruction in a Dockerfile is very different from using a volume (either via the -v
argument to docker run
or the VOLUME
instruction in a Dockerfile). The ADD
and COPY
commands just take a copy of the files at the time docker build
is run. These files are not updated until a fresh image is created with the docker build
command. By contrast, using a volume is essentially saying "this directory should not be stored in the container image; instead use a directory on the host"; whenever a file inside a volume is changed, both the host and container will see it immediately.
I don't believe you can achieve what you want using volumes, you'll have to rethink your directory structure if you want to do this.
However, it's quite simple to achieve using COPY
(which should be preferred to ADD
). You can either use a .dockerignore
file to exclude the subdirectory, or you could COPY
all the files then do a RUN rm bla
to remove the subdirectory.
Remember that any files you add to image with COPY
or ADD
must be inside the build context i.e. in or below the directory you run docker build
from.
这篇关于将一个卷添加到Docker,但不包括一个子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!