18

please help me on this docker django configuration for serving static files.

my Django project running on Docker got some issues with delivering static files.

All static files for admin view is loading fine, but static files for client web view is throwing 404 Not found Error.

This is my docker.yml configuration details:

web:
  build: ./web
  expose:
    - "8000"
  links:
    - postgres:postgres
  volumes:
    - ./web:/usr/src/app
  ports:
    - "8000:8000"
  env_file: .env
  command: python manage.py runserver 0.0.0.0:8000

postgres:
  image: postgres:latest
  volumes:
    - /var/lib/postgresql
  ports:
    - "5432:5432"

update

This is the admin static file url will look like : http://developer.com:8000/static/admin/css/base.css and this is how client static file url looks like: http://developer.com:8000/static/css/base.css Where those admin folder in static directory is creator by running django command collectstatic

I have used this setting previously, and was working fine. But when I moved the project root folder to another directory seems have this issue.

I am totally stuck here, many many thanks for all your help and feedback.

2
  • What is yout static root and staticfile_dirs in settings.py?
    – cutteeth
    Jul 28, 2016 at 5:00
  • STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, "static") Only these two configurations are there.
    – Dipak
    Jul 28, 2016 at 5:02

3 Answers 3

16

This was issue with the STATICFILES_DIRS configuration in the settings.py file.

This setting defines the additional locations the staticfiles app will traverse if the FileSystemFinder finder is enabled, e.g. if you use the collectstatic or findstatic management command or use the static file serving view.

Following was the configuration in my settings.py:

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

Now I updated this code to:

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

And every files is loading fine.

Reference Link

4
  • This worked, thanks! Its strange why it would fail in the container. Just a NOTE: depending on the path you are using for static files you may need to add / at the end of static.
    – sam
    Nov 16, 2016 at 1:49
  • I’ve just encountred this. So did you remove STATIC__ROOT? Do you have a github example app handy please? Mar 5, 2019 at 20:49
  • @GlennSampson Yes I have removed the STATIC_ROOT fromt he settings.py
    – Dipak
    Mar 6, 2019 at 4:26
  • 11
    If I remove STATIC_ROOT, I get an error: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
    – opyate
    Sep 11, 2020 at 8:03
11

Use Whitenoise to make your life easier when dealing with static files in django.

1.If you are using docker-compose,add whitenoise to your requirements.txt file:

whitenoise==3.3.1

2.Add whitenoise to your middleware apps inside settings.py

MIDDLEWARE_CLASSES = [# 'django.middleware.security.SecurityMiddleware','whitenoise.middleware.WhiteNoiseMiddleware',# ...]

make sure that you add this below your security.SecurityMiddleware app

3.Finally, change the following variables inside settings.py

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

STATIC_URL = '/static/'

STATICFILES_DIRS = (os.path.join(BASE_DIR,'<app_name>/static'),os.path.join(BASE_DIR, 'static'),)

Be sure to replace with the name of your app. Note that this only applies if your static files are stored in(for example) my_project/app/static/app/.

Otherwise if your static folder is located in my_project/app/static:

STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
  1. Lastly disable the built-in django static file server as follows:

    INSTALLED_APPS = [
    # ...
    'whitenoise.runserver_nostatic',
    'django.contrib.staticfiles',
    # ...]
    
4
  • 1
    Tried this but still I get 404, do I need to execute collectstatic as well?
    – Shruti
    Jul 14, 2020 at 9:19
  • This is a great answer, and has solved my number one frustration with Django Nov 12, 2020 at 15:10
  • You saved me hours of time with a Docker integration, thank you so much!
    – Aaron B
    Apr 13, 2021 at 5:05
  • I wasted 2 hours and was pretty sure static file stuff was completely broken in Docker. Tried this out of desperation and it worked immediately. Godsend.
    – griffsterb
    Aug 19, 2021 at 19:43
0

As you have moved your project to another directory, there is a possibility that the path of your static directories are also different now. Django in most scenarios use apache, nginx or some other web servers to serve static files. One point to notice is that your static directory should be accessed publicly. I had gone through a problem like this before. What I did was I moved static dir to document root mentioned in apache config file.

So move your static files to the doc root of apache and update static directories in settings.py to refer to the static directory in your apache doc root. I hope this helps.

2
  • I have moved all my static folder to DocumentRoot Path, but not worked.
    – Dipak
    Jul 28, 2016 at 9:48
  • can you access your doc root in browser using url <server-ip> or <hostname>? Try accessing a css file or any static file. url would be like http://your-ip/static/css/bootstrap.css.
    – cutteeth
    Jul 28, 2016 at 10:25

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.