0

I'm running a Django application via Docker. How do I get past the following error message:

django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.

I do have STATIC_ROOT declared in my settings.py file

import environ
import os

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

env = environ.Env()
env_path = BASE_DIR / ".env"
if env_path.is_file():
    environ.Env.read_env(env_file=str(env_path))
...
STATICFILES_DIRS = [BASE_DIR / "static"]
STATIC_URL = '/static/'

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

Some web searching led me to believe the settings.py file isn't even being referenced, but I could be wrong. I think that's shown in the manage.py file below.

#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_aws.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

The files are created in Windows, but the Docker container is not running Windows. I was getting some errors related to line endings, so I threw in the first 4 lines of this Dockerfile:

FROM ubuntu:latest
RUN apt-get update && apt-get install -y dos2unix
COPY manage.py /manage-clean.py
RUN dos2unix /manage-clean.py && apt-get --purge remove -y dos2unix && rm -rf /var/lib/apt/lists/*

FROM python:3.10-slim-buster

# Open http port
EXPOSE 8000

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV DEBIAN_FRONTEND noninteractive

# Install pip and gunicorn web server
RUN pip install --no-cache-dir --upgrade pip
RUN pip install gunicorn==20.1.0

# Install requirements.txt
COPY requirements.txt /
RUN pip install --no-cache-dir -r /requirements.txt

# Moving application files
WORKDIR /app
COPY . /app

RUN python manage.py collectstatic --no-input

Here's my project directory. Master project is DJANGO-AWS with 2 sub projects django-aws-backend and django-aws-infrastructure.

enter image description here

Mark
  • 1,455
  • 3
  • 28
  • 51
  • 1
    Is that the application `Dockerfile`? I'm betting this will help https://stackoverflow.com/a/23215679/1489860 – Jeffrey Mixon May 15 '23 at 22:43
  • Does your `DJANGO_SETTINGS_MODULE` environment variable have the correct path to the settings file? settings file itself looks correct. – Marco May 15 '23 at 23:00
  • Show your project directory structure and specify where dockerfile and main settings.py are located. – Ivan Starostin May 16 '23 at 10:14
  • @Marco - I tried the following on the cmd line, but it didn't change anything when I build docker: set DJANGO_SETTINGS_MODULE=settings.py – Mark May 16 '23 at 11:27

1 Answers1

1

I'd say that problems start with your folder organization.

django_aws is the django project root folder but it contains additional files like Dockerfile. manage.py points to django_aws.settings which should lead to django_aws_backend/django_aws/django_aws/settings.py - to default app folder. But it does not because

  • you have 2 manage.py files
  • one of them is placed too deep and relative path defined in it to settings.py leads nowhere
  • this manage.py I guess was created to fix docker image build because dockerfile is located inside project directory and has COPY . /app command which cannot reach manage.py located outside of this folder
  • settings.py is not located in it's usual place inside django default/main app; which is fine if DJANGO_SETTINGS_MODULE points right to it. /django_aws/manage.py does not point to it.

My suggestion is

  • create additional folder django-aws-backend/src
  • move django_aws sources inside this folder
  • put manage.py right under src folder
  • move Dockerfile outside of src folder and alter COPY command e.g. COPY ./src /app
django-aws-backend
  /src
     /django-aws
       /django-aws
       settings.py
     manage.py
  Dockerfile
  .gitignore
Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39