# Optimized Docker Compose Configuration for Laravel E-commerce
# Performance improvements and resource optimization

services:
  app:
    build:
      context: .docker
      dockerfile: Dockerfile
    container_name: e_comm_app
    restart: unless-stopped
    working_dir: /var/www/
    volumes:
      - ./:/var/www
      - ./.docker/php/conf.d/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
      - ./.docker/php/conf.d/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini
      - ./.docker/php/conf.d/performance.ini:/usr/local/etc/php/conf.d/performance.ini
      - ./.docker/php/conf.d/memory.ini:/usr/local/etc/php/conf.d/memory.ini
      - ./.docker/php/conf.d/optimized.ini:/usr/local/etc/php/conf.d/optimized.ini
    networks:
      - laravelapp
    ports:
      - "8000:8000"
    healthcheck:
      test: ["CMD", "php", "artisan", "--version"]
      interval: 30s
      retries: 3
      start_period: 10s
      timeout: 10s
    depends_on:
      db:
        condition: service_started
      redis:
        condition: service_healthy
    environment:
      - PHP_OPCACHE_ENABLE=1
      - PHP_OPCACHE_MEMORY_CONSUMPTION=256
      - PHP_OPCACHE_MAX_ACCELERATED_FILES=8000
      - PHP_OPCACHE_REVALIDATE_FREQ=0
      - PHP_OPCACHE_VALIDATE_TIMESTAMPS=0
    deploy:
      resources:
        limits:
          memory: 1G
          cpus: '1.0'
        reservations:
          memory: 512M
          cpus: '0.5'

  db:
    image: mariadb:latest
    container_name: e_comm_mysql
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: homestead
      MYSQL_USER: homestead
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_INNODB_BUFFER_POOL_SIZE: 512M
      MYSQL_INNODB_LOG_FILE_SIZE: 128M
      MYSQL_INNODB_FLUSH_LOG_AT_TRX_COMMIT: 2
      MYSQL_QUERY_CACHE_SIZE: 64M
      MYSQL_QUERY_CACHE_TYPE: 1
      MYSQL_INNODB_THREAD_CONCURRENCY: 8
      MYSQL_INNODB_READ_IO_THREADS: 8
      MYSQL_INNODB_WRITE_IO_THREADS: 8
      MARIADB_AUTO_UPGRADE: 1
    volumes:
      - mysql_data:/var/lib/mysql
      - ./.docker/mysql/my.cnf:/etc/mysql/my.cnf
      - ./.docker/mysql/init:/docker-entrypoint-initdb.d
    networks:
      - laravelapp
    ports:
      - "3311:3306"
    deploy:
      resources:
        limits:
          memory: 2G
          cpus: '2.0'
        reservations:
          memory: 1G
          cpus: '1.0'

  nginx:
    image: nginx:alpine
    container_name: e_comm_nginx
    restart: unless-stopped
    ports:
      - "90:80"
    volumes:
      - ./:/var/www
      - ./.docker/nginx/optimized.conf:/etc/nginx/conf.d/default.conf
    networks:
      - laravelapp
    healthcheck:
      test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost/"]
      interval: 30s
      retries: 3
      start_period: 5s
      timeout: 10s
    depends_on:
      app:
        condition: service_healthy
    deploy:
      resources:
        limits:
          memory: 256M
          cpus: '0.5'
        reservations:
          memory: 128M
          cpus: '0.25'

  redis:
    image: redis:alpine
    container_name: e_comm_redis
    restart: unless-stopped
    command: redis-server --appendonly yes --maxmemory 512mb --maxmemory-policy allkeys-lru --save 900 1 --save 300 10 --save 60 10000
    networks:
      - laravelapp
    ports:
      - "6381:6379"
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 30s
      retries: 3
      start_period: 5s
      timeout: 10s
    deploy:
      resources:
        limits:
          memory: 512M
          cpus: '0.5'
        reservations:
          memory: 256M
          cpus: '0.25'

  elasticsearch:
    image: elasticsearch:8.11.0
    container_name: e_comm_elasticsearch
    restart: unless-stopped
    environment:
      - discovery.type=single-node
      - xpack.security.enabled=false
      - "ES_JAVA_OPTS=-Xms1g -Xmx1g"
      - bootstrap.memory_lock=true
    volumes:
      - elasticsearch_data:/usr/share/elasticsearch/data
    networks:
      - laravelapp
    ports:
      - "9200:9200"
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost:9200/_cluster/health || exit 1"]
      interval: 30s
      retries: 3
      start_period: 60s
      timeout: 10s
    deploy:
      resources:
        limits:
          memory: 2G
          cpus: '2.0'
        reservations:
          memory: 1G
          cpus: '1.0'
    ulimits:
      memlock:
        soft: -1
        hard: -1

networks:
  laravelapp:
    driver: bridge
    driver_opts:
      com.docker.network.bridge.name: laravelapp

volumes:
  mysql_data:
    driver: local
  redis_data:
    driver: local
  elasticsearch_data:
    driver: local
