Docker挂载到文件夹覆盖内容 [英] Docker mount to folder overriding content

查看:2391
本文介绍了Docker挂载到文件夹覆盖内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.net Core Web Api,其配置文件位于名为Config的文件夹下. 我从中创建了图像和一个容器,然后在终端上正确看到了该容器,其中包含文件夹和配置文件.

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.

我的问题是,到目前为止,按照要求,我找不到一种将Config文件夹安装/绑定到物理路径的相同容器的方法:

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)将Config文件夹安装到特定的主机位置

1) Mount Config folder to a specific host location

2)在创建容器时,应在Config文件夹中填充图像中的文件

2) On container creation, the Config folder should be filled with the files in the image

3)在创建容器时,请使用图像中的文件覆盖文件夹中已经存在的所有现有文件

3) On container creation, override any existing file already present in the folder with those in the image

4)能够从主机自定义文件夹中的配置文件

4) Be able to customize the config files in the folder from the host

我的创建命令:

    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    

按照文档中的说明,使用类型为 bind -mount ,如果文件夹中有文件,则这些文件会隐藏在容器和应用程序中将看到已部署的文件. 此解决方案的问题是我无法从主机更新config文件夹中的文件.

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.

现在,删除 type = bind 我得到的结果是一样的,而且令人困惑.

Now, removing type=bind I get the same result, and it is confusing.

我尝试使用卷-volume $ configVolumePath:/app/Config:rw" ,但是这样做不会覆盖主机目录中的现有文件,而这些文件是将在容器内使用.

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.

其他说明,我没有在Dockerfile中指定任何内容,也没有编写与卷安装相关的内容,并且我也没有尝试创建一个卷来将其用作源,但是我认为这不会使差异.

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.

容器服务器正在NAS上运行,这是版本:

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

很明显我缺少了一些东西,我必须了解有关docker的更多信息,有人可以帮忙吗?

Clearly I'm missing something and I have to learn more about docker, can anyone help?

我的参考资料:

1) https://docs.docker.com/engine/admin /volumes/bind-mounts/

2) https://docs.docker.com/engine/admin/volumes /volumes/

推荐答案

首先,Docker卷或绑定挂载的行为类似于linux挂载.

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 refelected inside the container.

根据您的情况,您可以执行以下操作:

In your case, you can do the following:

docker volume create --driver local \
    --opt type=none \
    --opt device=$configVolumePath \
    --opt o=bind \
    config_vol

这将创建一个卷,该卷将保留在主机上的$ configVolumePath中.

This will create a volume which will be persisted in $configVolumePath on the host.

在创建容器时使用该卷:

When creating the container use that volume:

docker create --volume config_vol:/app/Config

您将得到的是启动,主机文件夹将为空,并且映像中的文件将被复制"到主机文件夹中. $configVolumePath中的编辑文件将放在容器内部,并且类似 在容器/app/Config中编辑的文件将反映在主机上的$configVolumePath中.

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 relected inside the container and similarly files edited inside the container /app/Config will be reflected in $configVolumePath on the host.

这篇关于Docker挂载到文件夹覆盖内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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