[Django]-404 error when accessing Django STATIC resources inside Docker image

2👍

Did you check for the permissions of the created static folder?

I had to manually change the permissions of the folders.

you could try with following Dockerfile for nginx:

FROM nginx:latest

RUN apt-get update && apt-get install -y procps
RUN mkdir -p /home/app/staticfiles
RUN chmod -R 755 /home/app/staticfiles
👤Toms

0👍

Could you try using this config.

Django:

STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)

Dockerfile:

RUN mkdir -p /home/appuser/app/staticfiles

Nginx.conf

location /static/ {
    alias /home/appuser/app/staticfiles/;
}

docker-compouse.yml something like:

web:
container_name: test_table_django
build: .
command:
  sh -c "python manage.py collectstatic --no-input --clear &&
    python manage.py migrate &&
    gunicorn --workers=4 --bind=0.0.0.0:8000 test_table.wsgi:application"
volumes:
    - .:/home/appuser/app
    - static_volume:/home/appuser/app/staticfiles/
env_file:
  - .env.prod
expose:
  - 8000
depends_on:
  - db
restart: always

nginx:
    build: ./nginx
    container_name: test_table_nginx
    ports:
      - 80:80
    volumes:
      - static_volume:/home/appuser/app/staticfiles/

0👍

The static URL should start with /.
You can also check the logs to see where it is trying to reach to.

BASE_DIR = Path(__file__).resolve().parent.parent.parent

STATIC_URL = '/static/'

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

Leave a comment