@DTPlayer

Почему сайт, развернутый в Docker Django не работает?

При обращении к серверу он отдает HTTP 502

docker compose
version: '3'

services:
  web:
    build: .
    depends_on:
      - postgres
      - certbot
    volumes:
      - static_volume:/app/static
    ports:
      - 8000:8000
      - 80:80
      - 443:443
    env_file:
      - .env
    environment:
      - DOMAIN=${DOMAIN}
      - EMAIL=${EMAIL}
      - IP=${IP}
      - DB_NAME=${DB_NAME}
      - DB_USER=${DB_USER}
      - DB_PASSWORD=${DB_PASSWORD}
      - DB_HOST=${DB_HOST}
      - DB_PORT=${DB_PORT}
      - DJANGO_SETTINGS_MODULE=tgbotshop.settings
      - TZ=${TZ}
      - PORT=${PORT}
      - CERTBOT_NONINTERACTIVE=${CERTBOT_NONINTERACTIVE}
      - CERTBOT_EMAIL=${CERTBOT_EMAIL}
      - CERTBOT_AGREE_TO_TOS=${CERTBOT_AGREE_TO_TOS}
    networks:
      - tgbotshop_net
    restart: always

  postgres:
    image: postgres:latest
    environment:
      - POSTGRES_DB=${DB_NAME}
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    networks:
      - tgbotshop_net

  nginx:
    image: nginx:latest
    depends_on:
      - web
    volumes:
      - static_volume:/app/static
      - ./tgbotshop.conf:/etc/nginx/conf.d/tgbotshop.conf
    networks:
      - tgbotshop_net
    restart: always

volumes:
  static_volume:
  postgres_data:

networks:
  tgbotshop_net:


Dockerfile
FROM python:3.11


RUN apt-get update \
    && apt-get install -y gcc python3-dev libpq-dev \
    && apt-get clean


COPY requirements.txt /tmp/
RUN pip install --no-cache-dir -r /tmp/requirements.txt

WORKDIR /app
COPY . /app/
RUN python manage.py collectstatic --noinput


COPY tgbotshop.conf /etc/nginx/conf.d/tgbotshop.conf

# Настройка среды
ENV DOMAIN=domain.ru \
    EMAIL=admin@domain.ru \
    IP=server ip \
    DB_NAME=db \
    DB_USER=user \
    DB_PASSWORD=pwd\
    DB_HOST=db \
    DB_PORT=5432 \
    DJANGO_SETTINGS_MODULE=tgbotshop.settings \
    TZ=Europe/Moscow \
    PORT=8000 \
    CERTBOT_NONINTERACTIVE=true \
    CERTBOT_EMAIL=admin@domain.ru \
    CERTBOT_AGREE_TO_TOS=true


# Открытие порта 80 и 443 для nginx
EXPOSE 80
EXPOSE 443

# Запуск приложения
CMD ["sh", "-c", "python manage.py migrate && gunicorn tgbotshop.wsgi:application --bind 127.0.0.1:8000"]


tgbotshop.conf
server {
    listen 80;
    server_name domain;

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

    location / {
        proxy_pass http://web:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

ssl сертификат будет получен от cloudflare (пытался запустить через certbot, но он не мог проверить домен)
Докер запускает все контейнеры, вот что в логах:
bot-web-1  | Operations to perform:
bot-web-1  |   Apply all migrations: admin, auth, bot, contenttypes, payments, sessions
bot-web-1  | Running migrations:
bot-web-1  |   No migrations to apply.
bot-web-1  | [2023-05-11 20:42:45 +0300] [21] [INFO] Starting gunicorn 20.1.0
bot-web-1  | [2023-05-11 20:42:45 +0300] [21] [INFO] Listening at: http://127.0.0.1:8000 (21)
bot-web-1  | [2023-05-11 20:42:45 +0300] [21] [INFO] Using worker: sync
bot-web-1  | [2023-05-11 20:42:45 +0300] [22] [INFO] Booting worker with pid: 22
bot-postgres-1  | 
bot-postgres-1  | PostgreSQL Database directory appears to contain a database; Skipping initialization
bot-postgres-1  | 
bot-postgres-1  | 2023-05-11 17:20:45.520 UTC [1] LOG:  starting PostgreSQL 15.2 (Debian 15.2-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
bot-postgres-1  | 2023-05-11 17:20:45.520 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
bot-postgres-1  | 2023-05-11 17:20:45.520 UTC [1] LOG:  listening on IPv6 address "::", port 5432
bot-postgres-1  | 2023-05-11 17:20:45.522 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
bot-postgres-1  | 2023-05-11 17:20:45.525 UTC [28] LOG:  database system was shut down at 2023-05-11 17:20:45 UTC
bot-postgres-1  | 2023-05-11 17:20:45.527 UTC [1] LOG:  database system is ready to accept connections
bot-postgres-1  | 2023-05-11 17:25:45.624 UTC [26] LOG:  checkpoint starting: time
bot-postgres-1  | 2023-05-11 17:26:01.875 UTC [26] LOG:  checkpoint complete: wrote 165 buffers (1.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=16.245 s, sync=0.003 s, total=16.251 s; sync files=76, longest=0.001 s, average=0.001 s; distance=781 kB, estimate=781 kB
bot-certbot-1   | Saving debug log to /var/log/letsencrypt/letsencrypt.log
bot-certbot-1   | 
bot-certbot-1   | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bot-certbot-1   | No renewals were attempted.
bot-certbot-1   | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bot-nginx-1     | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
bot-nginx-1     | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
bot-nginx-1     | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
bot-nginx-1     | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
bot-nginx-1     | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
bot-nginx-1     | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
bot-nginx-1     | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
bot-nginx-1     | /docker-entrypoint.sh: Configuration complete; ready for start up
bot-nginx-1     | 2023/05/11 17:42:45 [notice] 1#1: using the "epoll" event method
bot-nginx-1     | 2023/05/11 17:42:45 [notice] 1#1: nginx/1.23.4
bot-nginx-1     | 2023/05/11 17:42:45 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
bot-nginx-1     | 2023/05/11 17:42:45 [notice] 1#1: OS: Linux 5.4.0-139-generic
bot-nginx-1     | 2023/05/11 17:42:45 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
bot-nginx-1     | 2023/05/11 17:42:45 [notice] 1#1: start worker processes
bot-nginx-1     | 2023/05/11 17:42:45 [notice] 1#1: start worker process 29
bot-nginx-1     | 2023/05/11 17:42:45 [notice] 1#1: start worker process 30

В докере только начал разбираться, заранее спасибо за ваши ответы
  • Вопрос задан
  • 417 просмотров
Решения вопроса 1
Mi11er
@Mi11er
A human...
  1. gunicorn tgbotshop.wsgi:application --bind 127.0.0.1:8000 - Заменить на 0.0.0.0
  2. в nginx прописать web:8000
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы