Django application (runs in docker container) does not load static files

I’m running a django application in a docker container but the static files are not loading. I run the app through the django server itself, with DEBUG activated, the static files folder are in the root of the project, and “django.contrib.staticfiles” is present in my settings, even so, the css and javascript are not found .

Detail, this application works perfectly on my local machine, but when I run it through the Docker container, the static files do not work. Below are details of my project:

Logs docker container

some parts of settings django

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'bootstrapform',
]

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_FILES_DIRS = [
     os.path.join(BASE_DIR, 'static'),
]
LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = 'home'

docker compose

services:
  app:
    build: .
    restart: always
    command: python manage.py runserver 0.0.0.0:9000
    ports:
      - "9000:9000"
    volumes:
      - ./app:/code 

dockerfile

FROM python:3.8
WORKDIR /code
COPY requirements.txt /code
RUN pip install --no-cache-dir -r requirements.txt
COPY ./app /code

I assigned permission 755 in the static files directory, I changed the ports in docker compose, updated the python/django version and nothing solved it…

Can anyone explain to me what could be going on?

First things first, running the development server in production is a security and scaling nightmare. Don’t do that (Here the deployment docs for reference). If you’re just testing or developing, then sure :slight_smile: thats fine.

If i had to guess, you’re probably not running the container instance with DEBUG=True.

Feel free to share your settings.py (please make sure you remove any identifiable information such as the SECRET_KEY beforehand).

Your urls.py could also be the culprit. But there is no telling without the actual code or more specific error messages.

Hello, for now I’m just testing the application in docker, but this problem is preventing me from moving forward. DEBUG is enabled. As for urls.py, here is its contents.

from django.contrib import admin
from django.contrib.auth import views
from django.urls import path, include
from seca.views import my_logout, home

urlpatterns = [
    path('login/', views.LoginView.as_view(), name='login'),
    path('logout/', my_logout, name='logout'),
    path('', home, name='home'),
    path('pessoas/', include('pessoas.urls')),
    path('apiarios/', include('apiarios.urls')),
    path('revisoes/', include('revisoes.urls')),
    path('graficos/', include('graficos.urls')),
    path('admin/', admin.site.urls)
]

When DEBUG = True, then you also have to actually include the development static file serve urls.

Include this in your urls.py when to add the appropriate url(s) to the urlpatterns:

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

You’ll also have to import settings at the start of your urls.py for this to work, here is the complete example:

from django.conf import settings
from django.contrib import admin
from django.contrib.auth import views
from django.urls import path, include
from seca.views import my_logout, home

urlpatterns = [
    path('login/', views.LoginView.as_view(), name='login'),
    path('logout/', my_logout, name='logout'),
    path('', home, name='home'),
    path('pessoas/', include('pessoas.urls')),
    path('apiarios/', include('apiarios.urls')),
    path('revisoes/', include('revisoes.urls')),
    path('graficos/', include('graficos.urls')),
    path('admin/', admin.site.urls)
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

While you are at it you might as well give the docs for static file serving a shot: How to manage static files (e.g. images, JavaScript, CSS) | Django documentation | Django

1 Like

Thank you @JaK, at the moment this project is stopped, but I’ll test it later and any news I’ll post here !