Docker挂载到覆盖内容的文件夹
我有一个 .net Core Web Api,其配置文件位于名为 Config 的文件夹下。 我创建了图像和一个容器,并且我使用终端正确地看到该容器包含其中的文件夹和配置文件。
我的问题是,到目前为止,我找不到创建相同容器的方法,按照要求安装/绑定 Config 文件夹到物理路径:
1) 将 Config 文件夹安装到特定主机位置
2) 在创建容器时,配置文件夹应填充映像中的文件
3) 创建容器时,用映像中的文件覆盖文件夹中已存在的任何现有文件
4) 能够从主机自定义文件夹中的配置文件
我的创建命令:
docker --tls -H="$containerUrl" `
create `
--hostname $hostname `
--name $containerName `
--mac-address=$containerMacAddress `
--ip $containerIpAddress `
--net "bridged-network" `
--workdir '/app' `
--mount type=bind,src=$configVolumePath,target=/app/Config `
--publish "0.0.0.0::80" `
-t `
-i $imageName":"$script:buildversion
使用--mount 类型为 bind,如文档中所指定,如果文件夹中有任何文件,这些文件会隐藏在容器内,应用程序将看到已部署的文件文件。 此解决方案的问题是我无法从主机更新 config 文件夹中的文件。
现在,删除 type=bind 我得到相同的结果,这令人困惑。
我尝试使用卷 --volume $configVolumePath":/app/Config:rw",但这样做主机目录中预先存在的文件不会被覆盖,并且这些文件将被使用容器内。
附加说明,我没有在 Dockerfile 中指定任何内容或与卷安装相关的 compose,并且我没有尝试创建一个卷然后将其用作源,但我认为这不会产生任何影响。
容器服务器在 NAS 上运行,这是版本:
Version: 1.11.2
API version: 1.23
Go version: go1.5.4
Git commit: 781516c
Built: Thu Aug 3 16:04:05 2017
OS/Arch: linux/amd64
显然我遗漏了一些东西,我必须了解有关 docker 的更多信息,有人可以帮忙吗?
我的参考文献:
1)https://docs.docker.com/engine/admin /volumes/bind-mounts/
I have a .net Core web Api having configurations files under a folder called Config.
I created the image and a container from it, and I correctly see using the terminal, that the container contains the folder and the configuration files inside.
My problem is that so far I couldn't find a way to create the same container mounting/binding the Config folder to a physical path, following the requirements:
1) Mount Config folder to a specific host location
2) On container creation, the Config folder should be filled with the files in the image
3) On container creation, override any existing file already present in the folder with those in the image
4) Be able to customize the config files in the folder from the host
My create command:
docker --tls -H="$containerUrl" `
create `
--hostname $hostname `
--name $containerName `
--mac-address=$containerMacAddress `
--ip $containerIpAddress `
--net "bridged-network" `
--workdir '/app' `
--mount type=bind,src=$configVolumePath,target=/app/Config `
--publish "0.0.0.0::80" `
-t `
-i $imageName":"$script:buildversion
Using --mount with type bind, as specified in the documentation, if there is any file in the folder, those hare hidden from within the container and the application will see the deployed files.
The problem of this solution is that I cannot update the files in the config folder from the host.
Now, removing type=bind I get the same result, and it is confusing.
I tried to use volume --volume $configVolumePath":/app/Config:rw", but doing so the pre exising files in the host directory are not overridden and those are the one that will be used within the container.
Additional notes, I don't specify anything in the Dockerfile or compose related to volume mounting, and I didn't try to create a volume to then use it as a source, but I don't think that would make a difference.
The container server is running on a NAS and this is the version:
Version: 1.11.2
API version: 1.23
Go version: go1.5.4
Git commit: 781516c
Built: Thu Aug 3 16:04:05 2017
OS/Arch: linux/amd64
Clearly I'm missing something and I have to learn more about docker, can anyone help?
My references:
1) https://docs.docker.com/engine/admin/volumes/bind-mounts/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,docker 卷或绑定挂载的行为类似于 Linux 挂载。
如果主机卷/挂载存在并且包含文件,它将“覆盖”容器中的任何内容。如果不是,容器文件将镜像到主机卷/挂载上,并且容器文件夹和主机将同步。
在这两种情况下,在主机上编辑文件将始终反映在容器内。
对于您的情况,您可以执行以下操作:
这将创建一个卷,该卷将保留在主机上的 $configVolumePath 中。
创建容器时使用该卷:
您将在启动时看到,主机文件夹将为空,并且映像中的文件将被“复制”到主机文件夹中。编辑
$configVolumePath
中的文件将反映在容器内部,类似地在容器
/app/Config
内编辑的文件将反映在主机上的$configVolumePath
中。First of all, docker volumes or bind mounts behave like linux mounts.
If the host volume/mount exists and contains files it will "override" whatever is in the container. If not the container files will be mirrored onto the host volume/mount and the container folder and the host will be in sync.
In both cases editing the files on the host will ALWAYS be reflected inside the container.
In your case, you can do the following:
This will create a volume which will be persisted in $configVolumePath on the host.
When creating the container use that volume:
What you will get is on startup, the host folder will be empty and the files from the image will be "copied" onto the host folder. Editing files in
$configVolumePath
will be reflected inside the container and similarlyfiles edited inside the container
/app/Config
will be reflected in$configVolumePath
on the host.