version: '3.8'

services:
  # 메인 AI 개발 환경
  ai-course-dev:
    build:
      context: .
      dockerfile: Dockerfile.dev
    container_name: ai_course_dev
    ports:
      - "8888:8888"  # Jupyter Lab
      - "8000:8000"  # FastAPI/Flask 앱
      - "5000:5000"  # MLflow UI
    volumes:
      # 코드와 데이터를 호스트와 동기화
      - .:/app
      - jupyter_data:/root/.jupyter
      - pip_cache:/root/.cache/pip
      - models_cache:/root/.cache/huggingface
    environment:
      - PYTHONPATH=/app
      - JUPYTER_ENABLE_LAB=yes
      - JUPYTER_TOKEN=ai_course_token
      - ENVIRONMENT=development
      - DEBUG=True
    env_file:
      - .env  # 환경변수 파일 (있는 경우)
    networks:
      - ai-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8888/lab"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # PostgreSQL 데이터베이스
  postgres:
    image: postgres:14-alpine
    container_name: ai_course_postgres
    environment:
      POSTGRES_DB: ai_course_db
      POSTGRES_USER: ai_user
      POSTGRES_PASSWORD: ai_password
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./scripts/init_db.sql:/docker-entrypoint-initdb.d/init.sql
    networks:
      - ai-network
    restart: unless-stopped

  # Redis (캐싱 및 세션 저장)
  redis:
    image: redis:7-alpine
    container_name: ai_course_redis
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    networks:
      - ai-network
    restart: unless-stopped
    command: redis-server --appendonly yes

  # MongoDB (문서 저장)
  mongodb:
    image: mongo:6.0
    container_name: ai_course_mongodb
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: admin123
      MONGO_INITDB_DATABASE: ai_course
    ports:
      - "27017:27017"
    volumes:
      - mongodb_data:/data/db
      - ./scripts/init_mongo.js:/docker-entrypoint-initdb.d/init.js
    networks:
      - ai-network
    restart: unless-stopped

  # MLflow Tracking Server
  mlflow:
    image: python:3.9-slim
    container_name: ai_course_mlflow
    ports:
      - "5001:5000"
    volumes:
      - mlflow_data:/mlflow
    environment:
      - MLFLOW_BACKEND_STORE_URI=sqlite:///mlflow/mlflow.db
      - MLFLOW_DEFAULT_ARTIFACT_ROOT=/mlflow/artifacts
    networks:
      - ai-network
    restart: unless-stopped
    command: >
      sh -c "pip install mlflow psycopg2-binary &&
             mlflow server 
             --backend-store-uri sqlite:///mlflow/mlflow.db
             --default-artifact-root /mlflow/artifacts
             --host 0.0.0.0
             --port 5000"

  # Nginx (리버스 프록시)
  nginx:
    image: nginx:alpine
    container_name: ai_course_nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/dev.conf:/etc/nginx/conf.d/default.conf
      - ./nginx/ssl:/etc/nginx/ssl
    networks:
      - ai-network
    restart: unless-stopped
    depends_on:
      - ai-course-dev

  # 벡터 데이터베이스 (Qdrant)
  qdrant:
    image: qdrant/qdrant:latest
    container_name: ai_course_qdrant
    ports:
      - "6333:6333"
      - "6334:6334"
    volumes:
      - qdrant_data:/qdrant/storage
    networks:
      - ai-network
    restart: unless-stopped

  # Elasticsearch (검색 엔진)
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:8.10.0
    container_name: ai_course_elasticsearch
    environment:
      - discovery.type=single-node
      - xpack.security.enabled=false
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ports:
      - "9200:9200"
      - "9300:9300"
    volumes:
      - elasticsearch_data:/usr/share/elasticsearch/data
    networks:
      - ai-network
    restart: unless-stopped

  # Kibana (Elasticsearch 대시보드)
  kibana:
    image: docker.elastic.co/kibana/kibana:8.10.0
    container_name: ai_course_kibana
    environment:
      - ELASTICSEARCH_HOSTS=http://elasticsearch:9200
    ports:
      - "5601:5601"
    networks:
      - ai-network
    restart: unless-stopped
    depends_on:
      - elasticsearch

# 네트워크 정의
networks:
  ai-network:
    driver: bridge
    name: ai_course_network

# 볼륨 정의
volumes:
  jupyter_data:
    name: ai_course_jupyter_data
  pip_cache:
    name: ai_course_pip_cache
  models_cache:
    name: ai_course_models_cache
  postgres_data:
    name: ai_course_postgres_data
  redis_data:
    name: ai_course_redis_data
  mongodb_data:
    name: ai_course_mongodb_data
  mlflow_data:
    name: ai_course_mlflow_data
  qdrant_data:
    name: ai_course_qdrant_data
  elasticsearch_data:
    name: ai_course_elasticsearch_data 