skip to Main Content

This is the Dockerfile for my Django project:

FROM python:3.10.5-alpine

ENV PYTHONDONTWRITEBYTECODEBYDEFAULT=1
ENV PYTHONUNBUFFERED=1

RUN adduser --disabled-password appuser

USER appuser

WORKDIR /home/appuser/app

COPY requirements.txt .

USER root

RUN python -m pip install --no-cache-dir --disable-pip-version-check --requirement requirements.txt

USER appuser

COPY . .

ENTRYPOINT [ "./entrypoint.sh" ]

And Django settings regarding static assets:

STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'static/'

And entrypoint.sh

#!/bin/sh

python manage.py makemigrations
python manage.py migrate
python manage.py collectstatic --no-input
gunicorn project.wsgi:application --bind=0.0.0.0:8000 --workers=4 --timeout=300 --log-level=debug --log-file=-

exec "$@"

When I start the container I shell into it and see that static folder is created and populated with admin staff.

However browsing http://127.0.0.1:8000/admin brings up the admin login page without any CSS and I get lots of 404 errors in the developer console.

I also changed STATIC_ROOT to /home/appuser/app/static/ and got the same.

Please assist.

3

Answers


  1. 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
    
    Login or Signup to reply.
  2. 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/
    
    Login or Signup to reply.
  3. 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')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search