Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker mount to folder overriding content

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/

like image 661
Norcino Avatar asked Dec 05 '17 23:12

Norcino


People also ask

How do I mount multiple files in a docker container?

To do this, you can run the container with a bind mount, which support individual files if you specify the path: If you have a lot of files to mount this way, you can put them all in the same host directory, and mount that directory to a different location in the container.

What is the difference between volume and bind Mount in Docker?

When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its absolute path on the host machine. By contrast, when you use a volume, a new directory is created within Docker’s storage directory on the host machine, and Docker manages that directory’s contents.

What is source and destination of Mount in Docker?

The source of the mount. For bind mounts, this is the path to the file or directory on the Docker daemon host. May be specified as source or src. The destination takes as its value the path where the file or directory is mounted in the container.

Does Docker automatically create a new directory when--Mount is used?

It is always created as a directory. If you use --mount to bind-mount a file or directory that does not yet exist on the Docker host, Docker does not automatically create it for you, but generates an error.


1 Answers

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 

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

like image 131
yamenk Avatar answered Sep 23 '22 08:09

yamenk