简体   繁体   English

Gunicorn在Docker化后找不到静态文件

[英]Gunicorn can't find staticfiles after dockerization

I am using docker 17.05.0-cs edge on Ubuntu 17.04 Zesty. 我在Ubuntu 17.04 Zesty上使用docker 17.05.0-cs edge。 I have a gunicorn/Django app that I built here . 在这里建立了gunicorn / Django应用程式。 It runs fine before being dockerized, but gunicorn can't see the staticfiles after docker build . 它运行被dockerized之前罚款,但gunicorn不能看到后staticfiles docker build Here is the Dockerfile : 这是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!

The app runs and displays the page, funny, it even applies the css templates while saying it can't find bootstrap.css or bootstrap.js . 该应用程序运行并显示页面,有趣的是,它甚至在说找不到bootstrap.cssbootstrap.js同时应用了css模板。 It does not execute the script to toggle the menu though. 它不会执行脚本来切换菜单。 The current templates are just an artifact of my initial build, the new ones won't even use bootstrap.js I'm pretty sure. 当前模板只是我最初构建的bootstrap.js我敢肯定,新模板甚至都不会使用bootstrap.js It runs but the toggle doesn't work which I know is a feature of bootstrap.js . 它可以运行,但是切换不起作用,我知道这是bootstrap.js的功能。 This is the out from sudo docker run -it -p 8000:8000 dockerized_site : 这是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

I appended the static file url pattern to urls.py , I am pretty sure this has to do with the container build, or perhaps settings.py . 我将静态文件url模式附加到urls.py ,我很确定这与容器构建或settings.py Perhaps I need to add an environment directory for the static files? 也许我需要为静态文件添加一个环境目录? Any help is, of course, appreciated. 任何帮助,当然,赞赏。

See https://docs.djangoproject.com/en/1.11/howto/static-files/deployment/ for static files deployment. 请参阅https://docs.djangoproject.com/zh-CN/1.11/howto/static-files/deployment/了解静态文件部署。

But you may want to serve your static files using a web server like Nginx or Apache. 但是,您可能希望使用Nginx或Apache之类的Web服务器来提供静态文件。 You should not use Gunicorn to serve static files particularly in production. 您不应该使用Gunicorn来提供静态文件,尤其是在生产中。 Gunicorn is a WSGI server made for dynamic content. Gunicorn是用于动态内容的WSGI服务器。

This is your problem: 这是你的问题:

Your css 's are working ok: 您的css工作正常:

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

Your js 's are not working (404): 您的js无法正常运行(404):

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

See the difference? 看到不同?

The leading / in the path of the /js / js路径中的前导/

This fixes your issue: 这可以解决您的问题:

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

I cloned your repo and fixed the templates and I've gotten to fix your problem. 我克隆了您的存储库并修复了模板,现在我已经解决了您的问题。 Please, fix the other <script> s 请修复其他<script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM