在dockerization之后,Gunicorn找不到静态文件

我在Ubuntu 17.04 Zesty上使用docker 17.05.0-cs edge。 我在这里build立了一个gunicorn / Django应用程序。 在docker化之前它运行良好,但是在docker docker build之后gunicorn不能看到静态文件。 这是Dockerfile

 # Dockerfile # FROM base image to build upon FROM phusion/baseimage # RUN install python and pip RUN apt-get update RUN apt-get install -y python python-pip python-dev # ENV set environment directory tree ENV PROJECT=mysite ENV CONTAINER_HOME=/opt ENV CONTAINER_PROJECT=$CONTAINER_HOME/$PROJECT # move to project WORKDIR WORKDIR $CONTAINER_PROJECT # COPY project to container directory COPY . $CONTAINER_PROJECT # RUN pip and install requirements RUN pip install -r requirements.txt # COPY start.sh into known container location COPY start.sh /start.sh # EXPOSE port 8000 to allow communication to/from server EXPOSE 8000 # CMD execute start.sh to start the server running. CMD ["/start.sh"] # done! 

应用程序运行并显示页面,有趣的是,它甚至应用了css模板,同时说它找不到bootstrap.cssbootstrap.js 。 它不会执行脚本来切换菜单。 目前的模板只是我最初的构build工具,新的甚至不会使用bootstrap.js我很确定。 它运行,但切换不起作用,我知道是bootstrap.js的function。 这是从sudo docker run -it -p 8000:8000 dockerized_site

 Starting Gunicorn. [2017-06-07 00:38:56 +0000] [1] [INFO] Starting gunicorn 19.6.0 [2017-06-07 00:38:56 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1) [2017-06-07 00:38:56 +0000] [1] [INFO] Using worker: sync [2017-06-07 00:38:56 +0000] [9] [INFO] Booting worker with pid: 9 [2017-06-07 00:38:56 +0000] [10] [INFO] Booting worker with pid: 10 [2017-06-07 00:38:56 +0000] [11] [INFO] Booting worker with pid: 11 Not Found: /js/bootstrap.min.js Not Found: /js/jquery.js Not Found: /js/bootstrap.min.js Not Found: /favicon.ico 

我将这个静态文件的url模式附加到了urls.py ,我很确定这与容器的构build或者settings.py 。 也许我需要添加一个静态文件的环境目录? 当然,任何帮助都是值得赞赏的。

这是你的问题:

你的css工作正常:

 <link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet"> 

你的js不工作(404):

 <script src="{% static '/js/jquery.js' %}"></script> 

看到不同?

领先的/在/ js的path

这解决了你的问题:

 <script src="{% static 'js/jquery.js' %}"></script> 

我克隆你的回购和修复模板,我已经得到解决你的问题。 请修复其他的<script>

有关静态文件部署,请参阅https://docs.djangoproject.com/en/1.11/howto/static-files/deployment/

但是你可能想要使用像Nginx或Apache这样的Web服务器来为你的静态文件提供服务。 你不应该使用Gunicorn服务静态文件,特别是在生产中。 Gunicorn是为dynamic内容制作的WSGI服务器。