Загрузка данных


version: '3.8'

services:
  # 1. БАЗА ДАННЫХ
  db:
    image: postgres:15-alpine
    env_file:
      - ./db/.db.env
    ports:
      - "5433:5432"  # На хосте используем 5433, чтобы не конфликтовать с вашей Windows
    volumes:
      - pg_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 3s
      timeout: 3s
      retries: 5

  # 2. СКРИПТ ИНИЦИАЛИЗАЦИИ
  db-init:
    build: ./backend
    env_file:
      - ./db/.db.env
    depends_on:
      db:
        condition: service_healthy
    # Перед запуском скриптов ставим утилиту socat, которая перенаправляет localhost:5432 на db:5432
    command: >
      sh -c "apk add --no-cache socat &&
             socat TCP-LISTEN:5432,fork TCP:db:5432 &
             sleep 1 &&
             python init_db.py && python init_data.py"

  # 3. БЭКЕНД ПРИЛОЖЕНИЕ (FastAPI)
  backend:
    build: ./backend
    env_file:
      - ./db/.db.env
    ports:
      - "8000:8000"
    depends_on:
      db-init:
        condition: service_completed_successfully
    # Точно так же пробрасываем мост внутри контейнера бэкенда, чтобы base.py видел базу на localhost
    command: >
      sh -c "apk add --no-cache socat curl &&
             socat TCP-LISTEN:5432,fork TCP:db:5432 &
             sleep 1 &&
             uvicorn main:app --host 0.0.0.0 --port 8000"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://127.0.0.1:8000/"]
      interval: 5s
      timeout: 5s
      retries: 3

  # 4. ФРОНТЕНД (NGINX)
  frontend:
    image: nginx:alpine
    ports:
      - "8189:80"  # Доступ к приложению в браузере
    volumes:
      - ./frontend/nginx.conf:/etc/nginx/conf.d/default.conf:ro
      - ./frontend/styles.css:/usr/share/nginx/html/static/styles.css:ro
    depends_on:
      backend:
        condition: service_healthy

volumes:
  pg_data: