卷连接现有的已填充主机和容器目录时会发生什么 [英] What happens when a volume links an existing populated host and container dir

查看:78
本文介绍了卷连接现有的已填充主机和容器目录时会发生什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索过文档,但没有找到任何内容,因此需要时间对其进行测试。但是为了将来快速参考...



主机文件夹中是否装有容器文件夹内容?



是相反的吗?



两个文件夹的内容都合并了吗? (在这种情况下:两个文件夹中都有相同名称的文件会发生什么?)



还是会产生错误?错误是在启动时抛出,还是在您尝试构建具有指向容器上现有填充文件夹的VOLUME的映像时抛出?



不在文档中:我是否必须在Dockerfile中将容器路径定义为VOLUME以便在启动容器时对其使用-v?我是否可以即时创建卷?

解决方案

当您运行容器并从主机安装卷时,在容器中看到的就是主机上的内容-主机上的卷安装点目录,因此如果映像中的目录中没有任何内容,它将被绕过。



使用此Dockerfile中的映像:

  FROM $ ub $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ c> 

在没有主机挂载的情况下运行时,映像内容将复制到该卷中:

 > docker run vol-test ls / vol 
from-container

但是从主机,您只会看到主机的内容:

 > ls $(pwd)/主机
来自主机
> docker run -v $(pwd)/ host:/ vol vol-test ls / vol主机





$ b

不,您不需要 VOLUME 指令。没有它的行为是相同的。


I've searched the docs but nothing came up so time to test it. But for a quick future reference...

Is the host folder populated with the container folder contents?

Is it the opposite?

Are both folder contents merged? (In that case: What happens when a file with the same name is in both folders?)

Or does it produce an error? Is the error thrown on launch or is it thrown when you try to build an image with a VOLUME pointing to an existing populated folder on the container?

Also, another thing that isn't in the docs: Do I have to define the container path as a VOLUME in the Dockerfile in order to use -v against it when launching the container or can I create volumes on the fly?

解决方案

When you run a container and mount a volume from the host, all you see in the container is what is on the host - the volume mount points at the host directory, so if there was anything in the directory in the image it gets bypassed.

With an image from this Dockerfile:

FROM ubuntu
WORKDIR /vol
RUN touch /vol/from-container
VOLUME /vol

When you run it without a host mount, the image contents get copied into the volume:

> docker run vol-test ls /vol
from-container 

But mount the volume from the host and you only see the host's content:

> ls $(pwd)/host
from-host
> docker run -v $(pwd)/host:/vol vol-test ls /vol                                                          
from-host

And no, you don't need the VOLUME instruction. The behaviour is the same without it.

这篇关于卷连接现有的已填充主机和容器目录时会发生什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆