# Docker Compose Configuration
# Multi-service web application stack

version: "3.8"

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    image: webapp:latest
    container_name: webapp_web
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./src:/var/www/html
      - ./nginx/conf.d:/etc/nginx/conf.d
      - static_assets:/var/www/html/static
    environment:
      - APP_ENV=production
      - APP_DEBUG=false
      - APP_KEY=base64:randomgeneratedkey==
      - DB_HOST=db
      - DB_PORT=5432
      - DB_DATABASE=webapp
      - DB_USERNAME=webapp_user
      - DB_PASSWORD=s3cur3_p@ssw0rd
      - REDIS_HOST=redis
      - REDIS_PORT=6379
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started
    networks:
      - app_network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  db:
    image: postgres:16-alpine
    container_name: webapp_db
    restart: unless-stopped
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./database/init:/docker-entrypoint-initdb.d
    environment:
      - POSTGRES_DB=webapp
      - POSTGRES_USER=webapp_user
      - POSTGRES_PASSWORD=s3cur3_p@ssw0rd
    networks:
      - app_network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U webapp_user -d webapp"]
      interval: 10s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7-alpine
    container_name: webapp_redis
    restart: unless-stopped
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru
    networks:
      - app_network

volumes:
  postgres_data:
    driver: local
  redis_data:
    driver: local
  static_assets:
    driver: local

networks:
  app_network:
    driver: bridge