MinAI - Về trang chủ
Lý thuyết
6/1335 phút
Đang tải...

Docker cho AI Applications

Docker patterns dac biet cho AI - GPU support, model caching, optimization

0

🎯 Mục tiêu bài học

TB5 min

AI applications có những yêu cầu đặc biệt: GPU access, model weights, lớn RAM. Bài này cover các patterns Docker đặc thù cho AI.

Sau bài này, bạn sẽ:

✅ Hiểu các challenges khi Dockerize AI applications ✅ Cấu hình GPU support với NVIDIA Docker ✅ Implement model caching strategy ✅ Build optimized Dockerfile cho AI apps ✅ Quản lý secrets và resource limits

1

🔍 AI Docker Challenges

TB5 min
Diagram
Đang vẽ diagram...

Checkpoint

Bạn đã nắm được các challenges đặc thù khi Dockerize AI apps chưa?

2

🛠️ GPU Support với NVIDIA Docker

TB5 min
dockerfile
1# GPU-enabled base image
2FROM nvidia/cuda:12.1.0-runtime-ubuntu22.04
3
4# Install Python
5RUN apt-get update && \
6 apt-get install -y python3.11 python3-pip && \
7 rm -rf /var/lib/apt/lists/*
8
9WORKDIR /app
10COPY requirements.txt .
11RUN pip install --no-cache-dir -r requirements.txt
12
13COPY . .
14CMD ["python3", "main.py"]
Bash
1# Run with GPU
2docker run --gpus all -p 8000:8000 ai-gpu-app:latest
3
4# Specific GPU
5docker run --gpus '"device=0"' -p 8000:8000 ai-gpu-app:latest

Checkpoint

Bạn đã hiểu cách cấu hình GPU access cho Docker containers chưa?

3

⚡ Model Caching Strategy

TB5 min
dockerfile
1FROM python:3.11-slim
2
3WORKDIR /app
4
5# Install deps
6COPY requirements.txt .
7RUN pip install --no-cache-dir -r requirements.txt
8
9# Pre-download models (cached in image)
10RUN python -c "from transformers import AutoModel; AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')"
11
12COPY . .
13CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
Bash
1# Or mount model cache volume
2docker run -d \
3 -v model-cache:/root/.cache/huggingface \
4 -p 8000:8000 \
5 ai-app:latest

Checkpoint

Bạn đã hiểu cách cache model weights trong Docker image hoặc volume chưa?

4

💻 Optimized AI Dockerfile

TB5 min
dockerfile
1# Optimized for AI API (no local models)
2FROM python:3.11-slim AS production
3
4WORKDIR /app
5
6# System deps
7RUN apt-get update && apt-get install -y \
8 curl \
9 && rm -rf /var/lib/apt/lists/*
10
11# Python deps
12COPY requirements.txt .
13RUN pip install --no-cache-dir -r requirements.txt
14
15# App code
16COPY app/ ./app/
17
18# Security
19RUN useradd -m -r appuser
20USER appuser
21
22# Config
23ENV PYTHONUNBUFFERED=1
24ENV PYTHONDONTWRITEBYTECODE=1
25
26EXPOSE 8000
27HEALTHCHECK --interval=30s --timeout=10s \
28 CMD curl -f http://localhost:8000/health || exit 1
29
30CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "2"]

Checkpoint

Bạn đã hiểu cách tối ưu Dockerfile cho AI API production chưa?

5

🔒 Docker Secrets

TB5 min
Bash
1# Never put API keys in Dockerfile!
2# Use secrets at runtime
3
4# Method 1: Environment variables
5docker run -e OPENAI_API_KEY=sk-xxx ai-app
6
7# Method 2: Secret file
8echo "sk-xxx" | docker secret create openai_key -
9
10# Method 3: Docker Compose secrets
11# See next lesson

Checkpoint

Bạn đã hiểu cách quản lý API keys an toàn với Docker chưa?

6

📐 Resource Limits

TB5 min
Bash
1# Limit memory and CPU
2docker run -d \
3 --memory=2g \
4 --cpus=2 \
5 --name ai-api \
6 -p 8000:8000 \
7 ai-app:latest

Checkpoint

Bạn đã hiểu cách giới hạn tài nguyên cho Docker containers chưa?

7

🛠️ Health Check Pattern

TB5 min
python.py
1# app/main.py
2from fastapi import FastAPI
3from langchain_openai import ChatOpenAI
4
5app = FastAPI()
6
7@app.get("/health")
8async def health():
9 return {"status": "healthy"}
10
11@app.get("/ready")
12async def readiness():
13 try:
14 llm = ChatOpenAI(model="gpt-4o-mini")
15 response = await llm.ainvoke("test")
16 return {"status": "ready", "llm": "connected"}
17 except Exception as e:
18 return {"status": "not_ready", "error": str(e)}

Checkpoint

Bạn đã hiểu sự khác biệt giữa health check và readiness check chưa?

8

⚡ Image Size Optimization

TB5 min
Bash
1# Compare sizes
2docker images
3# python:3.11 -> ~1GB
4# python:3.11-slim -> ~150MB
5# python:3.11-alpine -> ~50MB (may have issues with some packages)
6
7# Tips to reduce size:
8# 1. Use slim base images
9# 2. Multi-stage builds
10# 3. Clean apt cache
11# 4. Use --no-cache-dir for pip
12# 5. Remove unnecessary files

Checkpoint

Bạn đã biết cách chọn base image và tối ưu image size chưa?

9

🎯 Tổng kết

TB5 min

Bài tập thực hành

Hands-on Exercise
  1. Build optimized Docker image cho AI API
  2. Implement health check endpoints
  3. Setup resource limits
  4. Compare image sizes giữa các base images

Target: Production-ready Docker image dưới 300MB

Câu hỏi tự kiểm tra

  1. Khi Dockerize AI application, cần quản lý environment variables (API keys) như thế nào để đảm bảo security?
  2. Health check endpoint và readiness endpoint khác nhau như thế nào trong context của AI Docker containers?
  3. So sánh image sizes giữa python:3.11, python:3.11-slim và python:3.11-alpine — khi nào nên dùng loại nào?
  4. Resource limits (memory, CPU) cần được cấu hình như thế nào cho Docker containers chạy AI workloads?

🎉 Tuyệt vời! Bạn đã hoàn thành bài học Docker cho AI Apps!

Tiếp theo: Chúng ta sẽ học Docker Compose để orchestrate multi-service AI deployments.


🚀 Bài tiếp theo

Docker Compose va Multi-service →