Docker mount to folder overriding content

2019-03-27 09:41发布

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/

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

1条回答
啃猪蹄的小仙女
2楼-- · 2019-03-27 09:52

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

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

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.

查看更多
登录 后发表回答