Django static files not served with Docker

Viewed 1252

EDIT: RESOLVED

I was confused between local project and container architecture and had the same name for STATIC_ROOT path (where static should be put after collectstatic) and STATICFILES_DIRS where django should look for more static files (not inside app)

juste change folder name in my local projetc: static to staticfiles

and update STATICFILES_DIR = (os.path.join(BASE_DIR, 'staticfiles'),) in settings prod.py and it works

pfffffffuuuu !


EDIT

I've just seen that I've made an error STATICFILES_DIR instead of STATICFILES_DIRS but it doesn't change...

and it raise n advertising because STATICFILES_DIRS contain STATIC_ROOT

project architecture in web container (simplified)

- web container
    |_app
        |_core
             |_settings
        |_app1
             |_static
        |_app2
             |_static
        |_requirements
        |_static
        |_Dockerfile.prod
        |_entrypoint.prod.sh
        |_manage.py
    |_nginx
        |_Dockerfile
        |_nginx.conf
    |_.env.prod
    |_docker-compose.prod.yml

EDIT 19/11/2020

I've copied my css file into /usr/src/app/static/ folder in my web container to test that nginx could correctly serve static files

docker cp styles.css coverage-africa-on-docker_web_1:/usr/src/app/static/css/styles.css

And it works so that it confirm that issue come from the fact that collectstatic command do not collect static files from /my_project/app/static/ folder even if I specify Django to look in this folder using STATICFILES_DIR = (os.path.join(BASE_DIR, 'static'),) variable in settings prod.py (see below my first EDIT)

So I tried to python manage.py findstatic styles.css command in my web container and my file is not found

No matching file found for 'styles.css'.

So, this means that STATICFILES_DIR = (os.path.join(BASE_DIR, 'static'),) is the wrong path...

BASE_DIR return /usr/src/app/ path so in my understanding, it is the goog path...


EDIT

I run docker logs coverage-africa-on-docker_web_1 to see what happened and static files seems to be collected and copied in correct folder in container

but css, images, js files are not served

I run docker exec -it coverage-africa-on-docker_web_1 sh and go to '/usr/src/app/static' folder and I can see admin, debug_toolbar,randomization(one app that have subfolder static... but not images, css, js

139 static files copied to '/usr/src/app/static', 1 unmodified.

I've hadded STATICFILES_DIR = (os.path.join(BASE_DIR, 'static'),) in settings prod.py to force collectstatic look for static files in /my_project/app/static/ folder

but doesn't change anything...


please find below my app architecture.

static files are located at /my_project/app/static/

when i 'jump' into my web container docker exec -it my_project sh /usr/src/app/static folder do not contain static files

I guess problem is path to serve static files but what is wrong with my configuration?

- my_project
    |_app
        |_core
             |_wsqi.py
             |_settings
                 |_base.py 
                 |_dev.py
                 |_prod.py
             |_urls.py
        |_requirements
             |_base.txt
             |_dev.txt
             |_prod.txt
        |_static
        |_Dockerfile.prod
        |_entrypoint.prod.sh
        |_manage.py
        |_.dockerignore
    |_nginx
        |_Dockerfile
        |_nginx.conf
    |_.env.prod
    |_.gitignore
    |_docker-compose.prod.yml

prod.py

SITE_ID = 1

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

MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

docker-compose.prod.yml

version: '3.7'

services:
    web:
        restart: always
        build: 
            context: ./app
            dockerfile: Dockerfile.prod
        restart: always
        command: gunicorn core.wsgi:application --bind 0.0.0.0:8000
        volumes:
            - ./app:/usr/src/app
            - static_volume:/usr/src/app/static
            - media_volume:/usr/src/app/media
        expose:
            - 8000
        env_file:
            - ./.env.prod
        extra_hosts: 
            - "db:192........"
   
    nginx:
        build: ./nginx
        restart: always
        volumes:
            - static_volume:/usr/src/app/static
            - media_volume:/usr/src/app/media
        ports:
            - 1338:80
        depends_on: 
            - web
volumes:
    static_volume:
    media_volume:

nginx.conf

upstream app {
    server web:8000;
}

server {

    listen 80;
    server_name localhost;
    location / {
        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /static/ {
        alias /usr/src/app/static/;
    }

    location /media/ {
        alias /usr/src/app/media/;
    }

}
0 Answers
Related