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

查看:58
本文介绍了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    

使用 --mountbind 类型,如文档中指定的那样,如果文件夹中有任何文件,则这些文件将从容器和应用程序中隐藏将看到部署的文件.这个解决方案的问题是我无法从主机更新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/卷/

推荐答案

首先,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 reflected 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.

创建容器时使用该卷:

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

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

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