Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mount data volume to docker with read&write permission

I want to mount a host data volume to docker. But the container should have read and write permission to it, meantime, any changes on the data volumes should not affect the data in host.

I can image a solution that mount several data volumes to single folder, one is read only another is read and write. But only this second '-v' works in my command,

docker run -ti --name build_cent1 -v /codebase/:/code:ro -v /temp:/code:rw centos6:1.0 bash
like image 545
Xiaokun Avatar asked Apr 29 '15 04:04

Xiaokun


2 Answers

only this second '-v' works in my command,

That might be because both -v options attempt to mount host folders on the same container destination folder /code.

   -v /codebase/:/code:ro 
                 ^^^^^
   -v /temp:/code:rw
            ^^^^^   

You could mount those host folders in two separate folders within /code.
As in:

-v /codebase/:/code/base:ro -v /temp:/code/temp:rw.
like image 161
VonC Avatar answered Oct 29 '22 17:10

VonC


Normally in this case I think you ADD the folder to the Docker image, so that any container running it will have it in its (writeable) filesystem, but writes will go to a different layer.

You need to write a Dockerfile in the folder above the one you wish to use, which should look something like this:

FROM my/image
ADD codebase /codebase

Then you build the container using docker build -t some-name <path>. These steps could be added to the build scripts of your app (maybe you will find some plugin to help there). Then you can docker run some-name.

The downside is that there is one copy to do and the image creation, but should you launch many containers they will share the same copy of the layer in read-only and write their own modifications to independent layers above.

like image 26
Andrea Ratto Avatar answered Oct 29 '22 16:10

Andrea Ratto