'Django static Files not loaded on Docker?

I am using Django 3.0.6 and want to dockerize my project. everything is worked fine except static files. Static files are loaded completely when I run my project normally. But, after dockerization, they are not loaded and throw a 404 error. my project layout as follow:

    ├── nginx
    │   ├── Dockerfile
    │   └── nginx.conf
    ├── src
         ├── applications
         ├── configuration
         │   ├── asgi.py
         │   ├── __init__.py
         │   ├── __pycache__
         │   ├── settings.py
         │   ├── urls.py
         │   └── wsgi.py
         ├── Dockerfile
         ├── manage.py
         ├── requirements.txt
         ├── static
         │   ├── build
         │   ├── images
         │   └── vendors
         └── templates

settings.py:

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

Dockerfile:

FROM python:3.6
ENV PYTHONUNBUFFERED 1
ENV SRC_DIR=/var/www/project/src
RUN mkdir -p $SRC_DIR
WORKDIR $SRC_DIR
ADD ./requirements.txt $SRC_DIR/requirements.txt
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ADD . $SRC_DIR

docker-compose.yml:

version: '3'
services:
  web:
    build:
      context: ./src
      dockerfile: Dockerfile
    command: bash -c "python manage.py makemigrations && python manage.py migrate && gunicorn --bind 0.0.0.0:8000 -w 4 configuration.wsgi"
    env_file: ./src/.environment
    volumes:
      - ./src:/var/www/project/src
      - ./src/static:/var/www/project/src/static
    ports:
      - 8000:8000
    depends_on:
      - db
  nginx:
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
      - 80:80
    volumes:
      - ./src:/var/www/project/src
      - ./src/static:/var/www/project/src/static
    depends_on:
      - web

nginx.conf:

    upstream view {
        server web:8000;
    }

    server {
        listen 80;

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

        location /static/ {
                autoindex on;
                alias /var/www/project/src/static;
        }

   }

I don't know What the problem is. please help me to solve this problem.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source