Static files not served when using docker + django + nginx

Viewed 309

I am trying to dockerize a django project and use nginx to serve static files. Everything seems to be working except the static files are not served. When I go to the admin page, always get Not Found: /django_static/rest_framework/css/prettify.css for files in django_static folder.

folder structure:

  • backend
    • server
      • apps
      • django_static
      • media
      • ...
  • docker
    • backend
      • Dockerfile
      • wsgi-entrypoint.sh
    • nginx
      • production
        • default.conf
      • development
        • default.conf
      • Dockerfile

Here is my docker-compose:


services:
    nginx:
        build: ./docker/nginx
        ports:
            - 1234:80
        volumes:
            - ./docker/nginx/production:/etc/nginx/conf.d
            - django_static:/app/backend/server/django_static
            - media:/app/backend/server/media
        depends_on:
            - backend
        restart: "on-failure"
    backend:
        build:
            context: .
            dockerfile: ./docker/backend/Dockerfile
        entrypoint: /app/docker/backend/wsgi-entrypoint.sh
        ports:
            - "8001:8001"
        volumes:
            - django_static:/app/backend/server/django_static
            - media:/app/backend/server/media
        expose:
            - 8001

volumes:
    django_static:
    media:

dockerfile for django:

FROM python:3.7-slim

WORKDIR /app
ADD ./backend/requirements.txt /app/backend/
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN pip install --upgrade pip
RUN pip install gunicorn
RUN pip install -r backend/requirements.txt

ADD ./backend /app/backend
ADD ./docker /app/docker

wsgi-entrypoint.py

#!/bin/sh

until cd /app/backend/server
do
    echo "Waiting for server volume..."
done

until ./manage.py migrate
do
    echo "Waiting for db to be ready..."
    sleep 2
done

./manage.py collectstatic --noinput

gunicorn server.wsgi --bind 0.0.0.0:8001 --workers 1 --threads 1
#./manage.py runserver 0.0.0.0:8003

nginx docker file:

FROM nginx:1.19.0-alpine

RUN rm /etc/nginx/conf.d/default.conf
CMD ["nginx", "-g", "daemon off;"]

nginx config file:

upstream glf {
    server  0.0.0.0:8001;
}

server {
    listen 80;

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

    location /django_static/ {
        alias /app/backend/server/django_static/;
    }

    location /media/ {
        alias /app/backend/server/media/;
    }
}

settings.py

MEDIA_URL = '/media/'
STATIC_URL = '/django_static/'
STATIC_ROOT = BASE_DIR / 'django_static'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

Any hint is appreciated!

0 Answers
Related