Home > OS >  Django app isn't working with docker and nginx
Django app isn't working with docker and nginx

Time:04-17

I am trying to launch django docker nginx web application

I don't see any errors in docker-compose logs. Full log: https://pastebin.com/tUpf5Wv0 But local urls 0.0.0.0:80 or 127.0.0.1:80 are not working.

It seems that problem deals with nginx but I don't see any errors in logs. How can I find the reason of the problem?

Application structure (full code https://github.com/mascai/django_docker_nginx_template)

.
├── docker-compose.yml
├── project
│   ├── core_app
    ...
│   ├── nginx-conf.d
│   │   └── nginx-conf.conf
│   ├── parser_app
    ...

Dockerfile

FROM python:3.9-alpine

WORKDIR /project

COPY . .
RUN apk add --update --no-cache --virtual .tmp-build-deps \
    gcc libc-dev linux-headers postgresql-dev && \
    pip install --no-cache-dir -r requirements.txt

nginx-conf.conf

upstream app {
    server django:8000;
}

server {
    listen 80;
    server_name 127.0.0.1;

    location / {
        proxy_pass http://django:8000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /static/ {
        alias /var/www/html/static/;
    }
}

Docker compose file (looks fine)

version: '3.9'

services:
  django:
    build: ./project # path to Dockerfile
    command: sh -c "gunicorn --bind 0.0.0.0:8000 core_app.wsgi:application"
    volumes:
      - ./project:/project
      - static:/project/static
    expose:
      - 8000
    environment: 
      - DATABASE_URL=postgres://postgres:post222@db:5432/lk_potok_4"
      - DEBUG=1
  
  db:
    image: postgres:13-alpine
    volumes:
      - pg_data:/var/lib/postgresql/data/
    expose: 
      - 5432
    environment: 
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=MY_PASSWORD
      - POSTGRES_DB=lk_potok_4
  
  nginx:
    image: nginx:1.19.8-alpine
    depends_on: 
      - django
    ports: 
      - "80:80"
    volumes:
      - static:/var/www/html/static
      - ./nginx-conf.d/:/etc/nginx/conf.d

volumes:
    pg_data:
    static:

CodePudding user response:

Found out that problem deals with invalid volumes in docker-compose file The fix: ./project/nginx-conf.d/:/etc/nginx/conf.d

This version is working for me:

version: '3.9'

services:
  django:
    build: ./project # path to Dockerfile
    command: sh -c "
      python manage.py makemigrations
      && python manage.py migrate  
      && gunicorn --bind 0.0.0.0:8000 core_app.wsgi"
    volumes:
      - ./project:/project
      - ./project/static:/project/static
    expose:
      - 8000
    environment: 
      - DATABASE_URL=postgres://postgres:post222@db:5432/lk_potok_4"
      - DEBUG=1
  
  db:
    image: postgres:13-alpine
    volumes:
      - pg_data:/var/lib/postgresql/data/
    expose: 
      - 5432
    environment: 
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=post222
      - POSTGRES_DB=lk_potok_4
  
  nginx:
    image: nginx:1.19.8-alpine
    depends_on: 
      - django
    ports: 
      - "80:80"
    volumes:
      - ./project/static:/var/www/html/static
      - ./project/nginx-conf.d/:/etc/nginx/conf.d
  
  celery:
    build: ./project
    command: celery -A core_app worker  --loglevel=info
    volumes:
      - ./project:/usr/src/app
    environment:
      - DEBUG=1
      - DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
      - CELERY_BROKER=redis://redis:6379/0
      - CELERY_BACKEND=redis://redis:6379/0
    depends_on:
      - django
      - redis

  redis:
    image: redis:5-alpine

volumes:
    pg_data:
    static:

  • Related