将文件从Docker容器暴露给主机 [英] expose files from docker container to host

查看:258
本文介绍了将文件从Docker容器暴露给主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存放Django应用的Docker容器。生成静态文件并将其复制到静态文件夹。

容器文件夹层次结构:

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

在构建Docker映像之前,我运行 ./manage.py collectstatic ,因此静态文件位于 / var / django / static 文件夹中。为了公开该应用程序并提供静态文件,我在主机上安装了nginx。问题是,如果我在主机的静态文件夹和指定文件夹之间进行卷操作,则当我运行docker容器时,该文件夹中的 / var / django / 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?

推荐答案

在Docker中,将卷视为装载,这与设置卷中的装载一样?意味着主机目录将始终挂载在容器目录上。换句话说,Docker卷目前无法实现。

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.

有关此主题的讨论,请参见Github问题: https://github.com/docker/docker/issues/4361

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

一个可能的解决方法是将Docker卷放入容器中的空目录,然后在Docker RUN命令(或启动脚本)中,将静态内容复制到该空目录中作为卷安装。

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.

这篇关于将文件从Docker容器暴露给主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆