version: '3.8'

services:
  # GPU 지원 AI 개발 환경
  ai-course-gpu:
    build:
      context: .
      dockerfile: Dockerfile.gpu
    container_name: ai_course_gpu
    ports:
      - "8888:8888"  # Jupyter Lab
      - "8000:8000"  # FastAPI/Flask 앱
      - "5000:5000"  # MLflow UI
      - "6006:6006"  # TensorBoard
    volumes:
      # 코드와 데이터를 호스트와 동기화
      - .:/app
      - jupyter_data:/root/.jupyter
      - pip_cache:/root/.cache/pip
      - models_cache:/root/.cache/huggingface
      - tensorboard_logs:/app/logs/tensorboard
    environment:
      - PYTHONPATH=/app
      - JUPYTER_ENABLE_LAB=yes
      - JUPYTER_TOKEN=ai_course_token
      - ENVIRONMENT=development
      - DEBUG=True
      - CUDA_VISIBLE_DEVICES=0  # GPU 0 사용
      - NVIDIA_VISIBLE_DEVICES=all
    env_file:
      - .env  # 환경변수 파일 (있는 경우)
    networks:
      - ai-network
    restart: unless-stopped
    # GPU 지원 설정
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: all
              capabilities: [gpu]
    # 또는 runtime 사용 (Docker Compose v2.3)
    # runtime: nvidia
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8888/lab"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # TensorBoard 서비스 (독립 실행)
  tensorboard:
    image: tensorflow/tensorflow:latest-gpu
    container_name: ai_course_tensorboard
    ports:
      - "6007:6006"
    volumes:
      - tensorboard_logs:/logs
    networks:
      - ai-network
    restart: unless-stopped
    command: tensorboard --logdir=/logs --host=0.0.0.0 --port=6006
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]

  # 나머지 서비스들은 기본 docker-compose.dev.yml과 동일
  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
    networks:
      - ai-network
    restart: unless-stopped

  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:
    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
    networks:
      - ai-network
    restart: unless-stopped

  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"

  # 벡터 데이터베이스 (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

# 네트워크 정의
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
  tensorboard_logs:
    name: ai_course_tensorboard_logs 