3

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:

Fuad Suleymanov
  • 149
  • 1
  • 2
  • 11
  • 1
    I checked your repo. In your Docker-compose file in the volumes you use `./static/:/amd/static` and `./media/:/amd/media`. But I cannot find the amd folder there. Is the path correct? – Apollo Mar 25 '21 at 12:14
  • 1
    Try synchronizing trailing slashes in volume mounts as described here https://stackoverflow.com/questions/38583900/mounted-volume-is-empty-inside-container – Ivan Starostin Mar 25 '21 at 15:59
  • @ctwx thanks for comment, but I try that method, but can't achieve any positive results. 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. – Fuad Suleymanov Mar 25 '21 at 18:54
  • @IvanStarostin thanks for help in my question editing, I change that trailing slashes but get same errors. 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. – Fuad Suleymanov Mar 25 '21 at 18:58
  • 1
    Check directory permissions then. – Ivan Starostin Mar 25 '21 at 21:50
  • @IvanStarostin my static and staticfiles diroctory was - **drwx------ 14 fuad fuad 4096 Mar 19 03:26 static** , **drwx------ 15 fuad fuad 4096 Mar 19 03:26 staticfiles** , I change him, now - **drwxrwxrwx 14 fuad fuad 4096 Mar 19 03:26 static** and **drwxrwxrwx 14 fuad fuad 4096 Mar 19 03:26 staticfiles** . but I had same problem. – Fuad Suleymanov Mar 26 '21 at 13:13

1 Answers1

0

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

Ondra
  • 943
  • 12
  • 19