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

Docker Basics

Cơ bản về Docker - containers, images, Dockerfile cho AI applications

0

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

TB5 min

Docker giúp package AI applications thành containers có thể chạy trên bất kỳ môi trường nào. Bài này cover Docker fundamentals cho AI deployment.

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

✅ Hiểu Docker architecture: images, containers, registry ✅ Viết Dockerfile cho Python AI applications ✅ Build và run Docker containers ✅ Sử dụng multi-stage builds để tối ưu image size ✅ Quản lý environment variables và volumes

1

🔍 Docker Architecture

TB5 min
Diagram
Đang vẽ diagram...
Docker Concepts
  • Image: Blueprint của application (read-only)
  • Container: Running instance của image
  • Dockerfile: Instructions để build image
  • Registry: Nơi lưu trữ images (Docker Hub, ECR)

Checkpoint

Bạn đã hiểu các khái niệm cơ bản của Docker (Image, Container, Dockerfile, Registry) chưa?

2

💻 Dockerfile cho Python AI App

TB5 min
dockerfile
1# Dockerfile
2FROM python:3.11-slim
3
4# Set working directory
5WORKDIR /app
6
7# Install system dependencies
8RUN apt-get update && apt-get install -y \
9 build-essential \
10 && rm -rf /var/lib/apt/lists/*
11
12# Copy requirements first (caching layer)
13COPY requirements.txt .
14RUN pip install --no-cache-dir -r requirements.txt
15
16# Copy application code
17COPY . .
18
19# Expose port
20EXPOSE 8000
21
22# Health check
23HEALTHCHECK --interval=30s --timeout=10s \
24 CMD curl -f http://localhost:8000/health || exit 1
25
26# Run application
27CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

Checkpoint

Bạn đã hiểu các instructions cơ bản trong Dockerfile chưa?

3

🛠️ Build và Run

TB5 min
Bash
1# Build image
2docker build -t ai-api:latest .
3
4# Run container
5docker run -d \
6 --name ai-api \
7 -p 8000:8000 \
8 -e OPENAI_API_KEY=sk-xxx \
9 ai-api:latest
10
11# Check status
12docker ps
13docker logs ai-api
14
15# Stop
16docker stop ai-api
17docker rm ai-api

Checkpoint

Bạn đã build và run được Docker container chưa?

4

⚡ Multi-stage Build

TB5 min
dockerfile
1# Stage 1: Build
2FROM python:3.11-slim AS builder
3
4WORKDIR /app
5COPY requirements.txt .
6RUN pip install --user --no-cache-dir -r requirements.txt
7
8# Stage 2: Production
9FROM python:3.11-slim AS production
10
11WORKDIR /app
12
13# Copy installed packages
14COPY --from=builder /root/.local /root/.local
15ENV PATH=/root/.local/bin:$PATH
16
17# Copy app
18COPY app/ ./app/
19
20# Non-root user
21RUN useradd -m appuser
22USER appuser
23
24EXPOSE 8000
25CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

Checkpoint

Bạn đã hiểu multi-stage builds giúp giảm image size như thế nào chưa?

5

🛠️ Environment Variables

TB5 min
Bash
1# .env file
2OPENAI_API_KEY=sk-xxx
3REDIS_URL=redis://redis:6379
4LOG_LEVEL=info
5MAX_TOKENS=1000
Bash
1# Run with env file
2docker run --env-file .env -p 8000:8000 ai-api:latest
3
4# Or individual env vars
5docker run \
6 -e OPENAI_API_KEY=sk-xxx \
7 -e LOG_LEVEL=debug \
8 -p 8000:8000 ai-api:latest

Checkpoint

Bạn đã hiểu cách truyền environment variables vào Docker container chưa?

6

📝 Volumes và Data

TB5 min
Bash
1# Mount volume for persistent data
2docker run -d \
3 -v $(pwd)/data:/app/data \
4 -v $(pwd)/logs:/app/logs \
5 -p 8000:8000 \
6 ai-api:latest

Docker Commands Cheat Sheet

Bash
1# Images
2docker images # List images
3docker build -t name:tag . # Build image
4docker rmi image_name # Remove image
5docker pull image_name # Pull from registry
6
7# Containers
8docker ps # Running containers
9docker ps -a # All containers
10docker exec -it name bash # Enter container
11docker logs -f name # Follow logs
12
13# Cleanup
14docker system prune # Clean unused
15docker image prune # Clean dangling images

.dockerignore

Ví dụ
1# .dockerignore
2__pycache__
3*.pyc
4.env
5.git
6.vscode
7node_modules
8*.md
9tests/
10.pytest_cache

Checkpoint

Bạn đã hiểu cách sử dụng volumes và các Docker commands phổ biến chưa?

7

🎯 Tổng kết

TB5 min

Best Practices

Docker Tips
  1. Layer caching: Put COPY requirements.txt trước COPY .
  2. Slim images: Dùng python:3.11-slim thay vì python:3.11
  3. Multi-stage: Giảm image size với multi-stage builds
  4. Non-root: Chạy container với non-root user
  5. Health checks: Add HEALTHCHECK cho monitoring
  6. .dockerignore: Exclude files không cần thiết

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

Hands-on Exercise
  1. Dockerize FastAPI AI app từ bài trước
  2. Build multi-stage image
  3. Run container với env variables
  4. Setup health check

Target: Docker image nhỏ hơn 500MB chạy được AI API

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

  1. Dockerfile có những instructions cơ bản nào (FROM, COPY, RUN, CMD) và mỗi instruction đóng vai trò gì?
  2. Tại sao layer caching lại quan trọng và việc đặt COPY requirements.txt trước COPY . giúp tối ưu build time như thế nào?
  3. Multi-stage builds giúp giảm Docker image size như thế nào? Hãy giải thích quy trình.
  4. Tại sao nên chạy container với non-root user và .dockerignore file có tác dụng gì?

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

Tiếp theo: Chúng ta sẽ áp dụng Docker chuyên sâu cho AI Applications.


🚀 Bài tiếp theo

Docker cho AI Applications →