I have set up my nginx folder, Dockerfile, .env, docker-compose.yml and entrypoint.sh files, and everything is running okay and I'm able to see pages as it should be. But the only problem is I can't load staticfiles. The gunicorn container logs are showing

"Not Found" and the nginx container is showing failed (2: No such file or directory).

These are my configuration files:

docker-compose.yml

version: '3'

services:
  marathon_gunicorn:
    volumes:
      - static:/static
    env_file:
      - .env
    build: 
      context: .
    ports:
      - "8010:8000"
      
  nginx:
    build: ./nginx
    volumes:
      - static:/static
    ports:
      - "8012:80"
    depends_on:
      - marathon_gunicorn

  db:
    image: postgres:15
    container_name: postgres_db
    restart: always
    environment:
      POSTGRES_DB: xxx
      POSTGRES_USER: xxx
      POSTGRES_PASSWORD: xxx
    volumes:
      - pg_data:/var/lib/postgresql/data

volumes:
  static:
  pg_data:`

entrypoint.sh

#!/bin/sh

# Apply database migrations
python manage.py migrate --no-input

# Collect static files
python manage.py collectstatic --no-input

# Start Gunicorn server
gunicorn absamarathon.wsgi:application --bind 0.0.0.0:8000

Django Dockerfile

FROM python:3.8

RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt

COPY ./absamarathon /app

WORKDIR /app

COPY ./entrypoint.sh /
ENTRYPOINT [ "sh", "/entrypoint.sh" ]

nginx/default.conf

upstream django {
    server marathon_gunicorn:8000;
}

server {
    listen 80;

    location / {
        proxy_pass http://django;
    }

    location /static/ {
    alias /static/;
}

    # location /media/ {
    #     alias /app/media/;
    # }
}

settings.py

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
3

There are 3 best solutions below

10
Mohamed ElKalioby On

Your ngix configuration isn't right as it doesn't handle static right, because you didn't tell nginx where is the folder to server /static. You shall put /static/ above / so it matches first.

server {
     listen 80;

     location /static/ {
        alias /app/static/;
      }

     location / {
        proxy_pass http://django;
      }

}

0
alex On

Create and attach volume to your container,

docker-compose.yml

volumes:
     static_files:
         name: static_files

nginx:
  build: ./nginx
  volumes:
    - static:/static
  ports:
    - "8012:80"
  depends_on:
    - marathon_gunicorn
  volumes:
    - static_files:/app/static:ro
            
0
IVNSTN On

Nginx config looks fine but virtual volume mapping for Django container is broken.

In Django docker image you defined

COPY ./absamarathon /app

thus your project BASE_DIR is /app folder. In Django project settings you defined

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

thus static files location is /app/static.

But in docker-compose config you map virtual volume static to container folder /static instead of /app/static.

For marathon_gunicorn container change volume mapping to

static:/app/static

Don't change it for Nginx container. Also you it may be helpful to see this answer on SO.