version: '3.8'

services:
  # MongoDB Database
  mongodb:
    image: mongo:7.0
    hostname: mongodb
    container_name: webscraper-mongodb
    restart: unless-stopped
    ports:
      - "27018:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: password123
      MONGO_INITDB_DATABASE: web_scraper
    volumes:
      - mongodb_data:/data/db
    networks:
      - scraper-network
    healthcheck:
      test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  mongo-express:
    image: mongo-express:1.0.2-20
    hostname: mongo-express
    container_name: webscraper-mongo-express
    restart: unless-stopped
    depends_on:
      mongodb:
        condition: service_healthy
    ports:
      - "8082:8081"
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: admin
      ME_CONFIG_MONGODB_ADMINPASSWORD: password123
      ME_CONFIG_MONGODB_URL: mongodb://admin:password123@mongodb:27017/
      ME_CONFIG_BASICAUTH_USERNAME: admin
      ME_CONFIG_BASICAUTH_PASSWORD: admin123
    networks:
      - scraper-network

  # Ollama LLM Service (CPU Optimized)
  ollama:
    image: ollama/ollama:latest
    hostname: ollama
    container_name: webscraper-ollama
    restart: unless-stopped
    ports:
      - "11434:11434"
    environment:
      - OLLAMA_HOST=0.0.0.0
      - OLLAMA_NUM_PARALLEL=2
      - OLLAMA_MAX_LOADED_MODELS=1
      - OLLAMA_CPU_THREADS=16
    volumes:
      - ollama_data:/root/.ollama
    networks:
      - scraper-network
    deploy:
      resources:
        limits:
          memory: 20G
          cpus: '16.0'
        reservations:
          memory: 10G
          cpus: '8.0'
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:11434/api/tags"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 120s


  web-scraper:
    build:
      context: .
      dockerfile: Dockerfile
    hostname: web-scraper
    container_name: webscraper-api-llm
    restart: unless-stopped
    ports:
      - "8006:8006"  # External:Internal port mapping
    environment:
      - MONGODB_URL=mongodb://admin:password123@mongodb:27017/web_scraper?authSource=admin
      - DEBUG=true
      - ENVIRONMENT=development
      - CUDA_VISIBLE_DEVICES=0  # Use first GPU if available
    depends_on:
      mongodb:
        condition: service_healthy
    networks:
      - scraper-network
    volumes:
      - ./logs:/app/logs
      - ./cache:/app/cache  # Cache for Hugging Face models
    deploy:
      resources:
        limits:
          memory: 12G  # Increased for LLM model loading
          cpus: '6.0'
        reservations:
          memory: 6G
          cpus: '3.0'
    # Uncomment the following lines if you have NVIDIA GPU support
    # runtime: nvidia
    # environment:
    #   - NVIDIA_VISIBLE_DEVICES=all
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 60s    # Increased interval
      timeout: 30s
      retries: 5       # More retries
      start_period: 600s  # 10 minutes startup time for model loading

  portainer:
    image: portainer/portainer-ce:latest
    hostname: portainer
    container_name: webscraper-portainer
    restart: unless-stopped
    ports:
      - "9001:9000"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - portainer_data:/data
    networks:
      - scraper-network

volumes:
  mongodb_data:
    driver: local
  portainer_data:
    driver: local
  ollama_data:
    driver: local

networks:
  scraper-network:
    driver: bridge