Search code examples
djangodockernginxdocker-composedjango-staticfiles

Permission denied, cannot open static files, Docker, Django


I am new in docker, I want dockerize my django application to production. My OS: Ubuntu 20.04.1 LTS, Docker version 20.10.5, build 55c4c88, My docker-compose.yml file as below:

version: '3.1'

services: 
    nginx-proxy:
        image: jwilder/nginx-proxy
        restart: "always"
        ports:
            - "80:80"
        volumes:
            - /var/run/docker.sock:/tmp/docker.sock:ro
            - ./nginx/vhost/:/etc/nginx/vhost.d:ro
            - ./nginx/conf.d/client_max_body_size.conf:/etc/nginx/conf.d/client_max_body_size.conf:ro
            - ./static/:/amd/static
            - ./media/:/amd/media
    postgres:
        image: postgres:9.6.6
        restart: always
        volumes:
            - ./pgdb/:/var/lib/postgresql/
        ports:
            - "5432:5432"
        env_file: ./.env

    redis:
        image: redis
        ports:
            - 6379:6379
        restart: always


    web:
        container_name: amd
        build: .
        restart: "always"
        ports:
            - "8000:8000"
        volumes:
            - .:/code/ 
            # - ./static/:/code/static
            # - ./media/:/code/media
        depends_on:
            - "postgres"
        env_file: .env

    celery:
        build:
            context: .
            dockerfile: celery.dockerfile
        volumes:
            - .:/code
        command: celery -A amdtelecom worker -l info   
        links:
            - redis
            - postgres
        depends_on:
            - "redis"
            - "postgres"
        env_file: ./.env

networks:
    default:
        external:
            name: nginx-proxy

My Dockerfile as below:

FROM python:3.7

ENV PYTHONUNBUFFERED 1
ENV DEBUG False

COPY requirements.txt /code/requirements.txt
WORKDIR /code
RUN pip install -r requirements.txt
ADD . .

CMD [ "gunicorn", "--bind", "0.0.0.0", "-p", "8000",  "amdtelecom.wsgi" ]

in my project setting file:


CORS_ALLOWED_ORIGINS = [
    "http://localhost:8000",
    "http://127.0.0.1:8000",
    "http://localhost"
]
STATIC_URL = '/static/'

if PROD:
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')
else:
    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    STATICFILES_DIRS = [
        BASE_DIR / "static",
    ]
    
    
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"

SITE_URL = 'http://localhost:80'

I use the below steps in my terminal:

  1. docker-compose up -d --build
  2. docker ps -a
  3. docker exec -it 'my gunicorn id' bash
  4. ./manage.py migrate
  5. ./manage.py collectstatic
  6. ./manage.py loaddata data.json

and open try open project in chrome browser in url localhost and project opens without static files (no js, no css). This is how it opens in the browser And in my docker logs:

> (docker-compose logs -f):
2021/03/25 10:51:32 [error] 51#51: *18 open() "/amd/static/images/fashion/product/13.jpg" failed (13: Permission denied), client: 172.23.0.1, server: localhost, request: 
"GET /static/images/fashion/product/13.jpg HTTP/1.1", host: "localhost", referrer: "http://localhost/"

docker logs and I want one note about my code - this application already work fine in Mac OS, static files open fine. I think my problem is related with my Linux OS.

How can I solve this problem?

Thanks in advance. For additional information about my code it is github repo:


Solution

  • The problem probably dwells in permissions, more exactly in file owners - you can solve it easily (but not much securely) with chmod 777 on all files in the static (or media) volume.

    The second option is to chown to the container user as decribed here https://stackoverflow.com/a/66910717/7113416 or propably even better here https://stackoverflow.com/a/56904335/7113416