Gunicorn can't find staticfiles after dockerization

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:

  • Reconstruct docker run command parameters from container
  • Permission denied in a Docker container even if user has the rights
  • Why are <none> images created and why doesn't Docker clean them up?
  • How do I access a private maven repo from inside a docker container?
  • Docker unable to connect AWS EC2 cloud
  • Exposing Docker Container Ports
  • 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.

  • docker port mapping error
  • Datastore for scope “global” is not initialized
  • Entity framework 7 with Microsoft SQL server not working in Docker
  • Docker: change directory command
  • Running Docker container randomly disappears on AWS EC2 Ubuntu
  • kube-dns not working on kubernetes arm
  • 2 Solutions collect form web for “Gunicorn can't find staticfiles after dockerization”

    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

    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.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.