Docker Volumes - Add Files Automatically

I am trying to create a docker container that has an external volume that should contain several folders, so my simplified version of the Dockerfile looks like this:

FROM ubuntu:12.04 # Create a volume for externally stored data that will persist across containers. VOLUME ["/uploads"] # Add the subfolders we need if they dont already exist # however this never works. RUN mkdir /uploads/folder1 RUN mkdir /uploads/folder2 

When I start the container with

 sudo docker run -i -t -v /uploads:/uploads [IMAGE ID] /bin/bash 

The / uploads folder does not contain folder1 or folder2. However, if I replace the line VOLUME uploads with RUN mkdir /uploads , it works with this command

 sudo docker run -i -t [IMAGE ID] /bin/bash 

but not with this command (folders are missing again):

 sudo docker run -i -t -v /uploads:/uploads [IMAGE ID] /bin/bash 

How to configure a docker file so that files / folders are automatically added to the directory with the host installed when the container starts?

+7
docker volume
source share
1 answer

How to configure a docker file so that files / folders are automatically added to the directory with the installed hosts when the container starts?

Not. The Docker file is used to create an image, to configure the contents of the image. You can configure the contents of your mounted directory directly in your shell:

 # create folders: mkdir /uploads123 mkdir /uploads123/folder1 mkdir /uploads123/folder2 # run container docker run -i -t -v /uploads123:/uploads [IMAGE ID] /bin/bash # for this trivial case, you can use directly ubuntu image, # it works, no need for Dockerfile 

Alternatively , you can set some configuration script to run in the container before starting the main process. This script setup can populate the installed volume using additional folders.

To explain the behavior - your RUN mkdir /uploads/folder1 created a folder on the image, but you shaded the folder with the volume installed, so you do not see this folder (the folder is in the image, not in the host folder), you cannot create folders on volume in its Docker file, because the volume is mounted at runtime later (container).

+9
source share

All Articles