Gunicorn can't find staticfiles after dockeriz

2019-08-02 01:18发布

问题:

I am using docker 17.05.0-cs edge on Ubuntu 17.04 Zesty. I have a gunicorn/Django app that I built here. It runs fine before being dockerized, but gunicorn can't see the staticfiles after docker build. Here is the 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. 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. It runs but the toggle doesn't work which I know is a feature of bootstrap.js. This is the out from 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. Perhaps I need to add an environment directory for the static files? Any help is, of course, appreciated.

回答1:

This is your problem:

Your css's are working ok:

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

Your js's are not working (404):

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

See the difference?

The leading / in the path of the /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



回答2:

See https://docs.djangoproject.com/en/1.11/howto/static-files/deployment/ for static files deployment.

But you may want to serve your static files using a web server like Nginx or Apache. You should not use Gunicorn to serve static files particularly in production. Gunicorn is a WSGI server made for dynamic content.