Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Volumes - Automatically adding files

Tags:

docker

volume

I am trying to create a docker container that has an external volume which 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

Whenever I launch 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 the VOLUME uploads line with a RUN mkdir /uploads it does work 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 can I set up the dockerfile so that files/folders will automatically get added to the hosts mounted directory upon running the container?

like image 895
Programster Avatar asked Apr 17 '14 11:04

Programster


Video Answer


1 Answers

How can I set up the dockerfile so that files/folders will automatically get added to the hosts mounted directory upon running the container?

You don't. Dockerfile is used to create an image, to set up content of the image. You can set up content 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 setup script to run in container before starting the main process. This setup script can populate mounted volume with nescessary folders.

To explain the behaviour - your command RUN mkdir /uploads/folder1 did create the folder in the image, but you shadowed the folder with mounted volume, so you do not see this folder (folder is in the image, not in the host folder). You cannot create folders on volume in your Dockerfile, because volume is mounted at runtime later (container).

like image 145
Jiri Avatar answered Nov 09 '22 09:11

Jiri