version: "3.8"

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
      target: production
      args:
        APP_ENV: production
        NODE_VERSION: 20
    image: my-application:latest
    container_name: myapp
    restart: unless-stopped
    ports:
      - "8080:80"
      - "8443:443"
    volumes:
      - ./storage:/var/www/html/storage
      - ./logs:/var/log/app
    environment:
      APP_ENV: production
      APP_DEBUG: "false"
      DB_HOST: database
      DB_PORT: 5432
      DB_DATABASE: myapp
      DB_USERNAME: postgres
      DB_PASSWORD: ${DB_PASSWORD}
      REDIS_HOST: cache
      REDIS_PORT: 6379
    depends_on:
      database:
        condition: service_healthy
      cache:
        condition: service_started
    networks:
      - backend
      - frontend
    deploy:
      replicas: 3
      resources:
        limits:
          cpus: "0.50"
          memory: 512M
        reservations:
          cpus: "0.25"
          memory: 256M
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  database:
    image: postgres:16-alpine
    container_name: myapp-db
    restart: unless-stopped
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./docker/postgres/init.sql:/docker-entrypoint-initdb.d/init.sql
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      PGDATA: /var/lib/postgresql/data/pgdata
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

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

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

networks:
  backend:
    driver: bridge
  frontend:
    driver: bridge
