Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker add files to VOLUME

I have a Dockerfile which copies some files into the container and after that creates a VOLUME.

...
ADD src/ /var/www/html/
VOLUME /var/www/html/files
...

In the src folder is an files folder and in this files folder are some files I need to have copied to the VOLUME the first time the container gets started.

I thought the first time the container gets created it uses the content of the original dir specified in the volume but this is not the case.

So how can I get the files into this folder? Do I need to create an extra folder and copy it with a runscript (I hope not)?

like image 611
blacksheep_2011 Avatar asked May 05 '17 15:05

blacksheep_2011


2 Answers

Whatever you put in your Dockerfile is just evaluated at build time (and not when you are creating a new container).

If you want to make file from the host available in your container use a data volume:

docker run -v /host_dir:/container_dir ...

In case you just want to copy files from the host to a container as a one-off operation you can use:

docker cp /host_dir mycontainer:/container_dir
like image 169
Sebastian Avatar answered Nov 12 '22 05:11

Sebastian


The issue is with your ADD statement. Also you might not understand how volumes are accessed. Compare your efforts with the demo below:

FROM alpine #, or your favorite tiny image
ADD src/files /var/www/html/files
VOLUME /var/www/html/files

Build an image called 'dataimg':

docker build -t dataimg .

Use the dataimg image to create a data container named 'datacon':

docker run --name datacon dataimg /bin/cat

Mount the volume from datacon in your nginx container:

docker run --volumes-from datacon nginx ls -la /var/www/html/files

And you'll see the listing of /var/www/html/files reflects the contents of src/files

like image 36
Slack Flag Avatar answered Nov 12 '22 05:11

Slack Flag