expose files from docker container to host

浪子不回头ぞ 提交于 2020-01-03 10:46:11

问题


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?


回答1:


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.




回答2:


Another nice solution: 1. Install SSHd 2. Install SSHFS on Host 3. Mount folder inside docker container to outside (host) by SSHFS




回答3:


Reading from: Dockers Volume Page

Volumes have several advantages over bind mounts:

New volumes can have their content pre-populated by a container.


Similar example using docker-compose

Using nginx's default webpage folder as the example:

$ docker volume create xmpl
$ docker run -v xmpl:/usr/share/nginx/html nginx

Will yield all the files on the host system via:

$ docker inspect xmpl
...
"Mountpoint": "/var/lib/docker/volumes/xmpl/_data"

And you can then view the files on the host:

# ls /var/lib/docker/volumes/xmpl/_data                               
  50x.html  index.html

And finally to use it from /var/nginx/static:

# mkdir -p /var/nginx
# ln -s /var/lib/docker/volumes/xmpl/_data /var/nginx/static
# ls /var/nginx/static
  50x.html  index.html


来源:https://stackoverflow.com/questions/38940811/expose-files-from-docker-container-to-host

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!