[Fixed]-How to make static files works using django docker nginx and postgresql since its not serving them


Nginx config

Nginx config looks fine. Container is mapped to /backend/languages/static with /static and the alias points to the same folder inside container – /static.


location /static/ {
        autoindex on;
        alias /static;

upd issue detected: an alias must have the same ending slash to work properly. so it has to be alias /static/

nginx in compose

      - ./backend/languages:/backend
      - ./backend/languages/static:/static

Django config

But Django config looks broken. Here you’ve configured to collect static to static folder within BASE_DIR


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

and this is exactly what collectstatic is reporting to you:

165 static files copied to '/backend/static'

files are in the /backend/static, not /static. Hovewer the container is configured to root folder /static:

django in compose

      - ./backend/languages:/backend
      - ./backend/languages/static:/static

looks like the issue can be fixed by pointing to

      - ./backend/languages/static:/backend/static

Nevertheless there is still some work to do: media files are not supposed to be served by Django as well, so it is recommended to configure Nginx to serve media files too.

Django Dockerfile

I believe the mapping ./backend/languages:/backend works but what’s the point of dockerizing then? There is no app in this docker image, just dependencies. It is "better" to include sources in the image so the deployment in the end would require to update images and restart containers only.

So, as a side note, I’d suggest to (at least try to):

  • update configs to serve media files with Nginx
  • include django app sources inside your docker image
  • collect static files during the build and not to include them into docker image
  • remove collectstatic from the entrypoint
  • supply static files and treat them as a separate product, think of them if you were going to deliver them to a CDN, a separate hosting – it is a very common solution
  • whilst they are still on the same hosting, keep mapping them to the container as you do now, but in a "reversed" way: deliver static files to the host folder, access them from the container in ro mode


You can use whitenoise by configuring it in your settings file. Set up Whitenoise with Django here

Leave a comment