Nginx and Gunicorn Static files not being found in my docker-compose django project even if logs show '125 static files copied to '/app/static''

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/;
    # }
}

nginx Dockerfile:

FROM nginx:1.19.0-alpine

COPY ./default.conf /etc/nginx/conf.d/default.conf

settings.py:

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

What are the complete error messages that you’re seeing in the nginx error log file?

Can you inspect the static volume to verify that the files are contained within it? (And with the appropriate directory structure that nginx is expecting to find.) It looks to me like you’re collecting the static files into the project and not the static volume. (From what I can tell here, STATIC_ROOT should probably be /static.)

1 Like

So in my gunicorn logs the general error with regards to static files is “ Not Found” and for the Nginx logs its “No such directory”

And when I try check even if it says all or 0 files static files have been copied when I run docker-compose up, no static files have been rendering on the browser.

So I provided my configiration files to get some help on what I could be doing wrong.

When I ask for the contents of the error logs, please copy the actual text of the error and paste into the body of your message. (Enclosed between lines of three backticks - ` , just like you would do with any other preformatted text.)

Again, please don’t try to summarize or describe the situation. If you are running commands to try things, copy/paste the text of the command and the output into your response. (Also between lines of ``` as described above.)

Right now, I’m thinking it’s because you have the wrong STATIC_ROOT defined in your settings. You’re defining a data volume to be shared between nginx and Django at the location /static. This means you would want the static files copied to /static, and not collected to a directory in your project.