Search code examples
djangodockernginxgunicorndjango-staticfiles

Django on docker; Staticfiles disappearing


I am trying to deploy my django project using docker nginx and gunicorn.
My Issue is that the staticfiles I use for my project are missing from the container.

when in the project:

ls staticfiles/
css  disconnected.html  images  lib  nothing.css

after going into the container with docker exec -it foo_web_1 /bin/sh

ls staticfiles/
admin

my docker-compose file:

version: '3.7'

services:
  web:
    build:
      context: ./foo-app
      dockerfile: Dockerfile.prod
    command: gunicorn foo-app.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - static_volume:/home/foo-app/web/staticfiles
      - media_volume:/home/foo-app/web/mediafiles
    expose:
      - 8000
    env_file:
      - ./.env.prod
  nginx:
    build: ./nginx
    volumes:
      - static_volume:/home/foo-app/web/staticfiles
      - media_volume:/home/foo-app/web/mediafiles
    ports:
      - 2375:80
    depends_on:
      - web
volumes:
  postgres_data:
  static_volume:
  media_volume:

the nginx.conf file:

upstream foo-app {
    server web:8000;
}

server {

    listen 80;

    location / {
        proxy_pass http://foo-app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /staticfiles/ {
        alias /home/foo-app/web/staticfiles/;
    }

    location /mediafiles/ {
        alias /home/foo-app/web/mediafiles/;
    }

}

the part of the settings.py that relates to staticfiles:

STATIC_URL = "/staticfiles/"
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "staticfiles")
]

Inside my dockerfile I define the following:

ENV HOME=/home/foo-app
ENV APP_HOME=/home/foo-app/web
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

and later I simply copy the entire project in using

COPY . $APP_HOME

All other file show up perfectly fine, and I have tried remove and rebuild the entire project to avoid artifacts to no avail.


Solution

  • The Problem got resolved by separating STATIC_ROOT from the location of my staticfiles and then adding python manage.py collectstatic --no-input --clear to my startupscript.