expose files from docker container to host

Mr T. picture Mr T. · Aug 14, 2016 · Viewed 7.2k times · Source

I have a docker container that holds a django app. The static files are produced and copied to a static folder.
container folder hierarchy:

- var
    - django
        - app
        - static

before i build the docker image, i run ./manage.py collectstatic so the static files are in the /var/django/static folder. To expose the app and serve the static files, i have on the host an nginx. The problem is that if i do a volume between the static folder and a designated folder on the host, when i run the docker container, the /var/django/static folder in the container gets deleted (well, not deleted but mounted). Is there any way to overcome this? as in set the volume but tell docker to take the current files as well?

Answer

C. Reed picture C. Reed · Aug 14, 2016

Volumes are treated as mounts in Docker, which means the host directory will always be mounted over the container's directory. In other words, what you're trying to do isn't currently possible with Docker volumes.

See this Github issue for a discussion on this subject: https://github.com/docker/docker/issues/4361

One possible work-around would be to have a docker volume to an empty directory in your container, and then in your Docker RUN command (or start-up script), copy the static contents into that empty directory that is mounted as a volume.