---
description: Docker 容器化配置和最佳实践
globs: ["**/Dockerfile", "**/docker-compose.*", "**/.dockerignore"]
alwaysApply: false
---

# Docker 容器化规范

## 🐳 前端 Dockerfile

### 多阶段构建
```dockerfile
# Dockerfile.frontend
# ✅ 多阶段构建 Dockerfile
# 构建阶段
FROM node:18-alpine AS builder

WORKDIR /app

# 复制包管理文件
COPY package*.json ./
COPY pnpm-lock.yaml ./

# 安装依赖
RUN npm install -g pnpm && pnpm install --frozen-lockfile

# 复制源代码
COPY . .

# 构建应用
RUN pnpm build

# 生产阶段
FROM nginx:alpine AS production

# 复制构建产物
COPY --from=builder /app/dist /usr/share/nginx/html

# 复制 Nginx 配置
COPY nginx.conf /etc/nginx/nginx.conf

# 暴露端口
EXPOSE 80

# 启动命令
CMD ["nginx", "-g", "daemon off;"]
```

## 🌐 Nginx 配置

### 生产环境配置
```nginx
# nginx.conf
events {
  worker_connections 1024;
}

http {
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  
  # ✅ Gzip 压缩
  gzip on;
  gzip_vary on;
  gzip_min_length 1024;
  gzip_types
    text/plain
    text/css
    text/xml
    text/javascript
    application/javascript
    application/xml+rss
    application/json;
  
  # ✅ 缓存策略
  location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
    expires 1y;
    add_header Cache-Control "public, immutable";
  }
  
  # ✅ SPA 路由支持
  location / {
    try_files $uri $uri/ /index.html;
  }
  
  # ✅ API 代理
  location /api/ {
    proxy_pass http://backend:3000/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
  }
  
  server {
    listen 80;
    server_name localhost;
    root /usr/share/nginx/html;
    index index.html;
  }
}
```

## 🚀 后端 Dockerfile

### NestJS 应用容器化
```dockerfile
# Dockerfile.backend
# ✅ NestJS 应用 Dockerfile
FROM node:18-alpine AS builder

WORKDIR /app

# 复制包管理文件
COPY package*.json ./
COPY pnpm-lock.yaml ./

# 安装依赖
RUN npm install -g pnpm && pnpm install --frozen-lockfile

# 复制源代码
COPY . .

# 构建应用
RUN pnpm build

# 生产阶段
FROM node:18-alpine AS production

WORKDIR /app

# 复制构建产物和依赖
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package*.json ./

# 创建非 root 用户
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nestjs -u 1001

# 更改文件所有者
RUN chown -R nestjs:nodejs /app
USER nestjs

# 暴露端口
EXPOSE 3000

# 启动命令
CMD ["node", "dist/main.js"]
```

## 🐙 Docker Compose 配置

### 完整服务编排
```yaml
# docker-compose.yml
version: '3.8'

services:
  # ✅ 前端服务
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    ports:
      - "80:80"
    depends_on:
      - backend
    environment:
      - VITE_API_URL=http://localhost:3000
    networks:
      - app-network

  # ✅ 后端服务
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    depends_on:
      - postgres
      - redis
    environment:
      - NODE_ENV=production
      - DATABASE_URL=postgresql://user:password@postgres:5432/mydb
      - REDIS_URL=redis://redis:6379
      - JWT_SECRET=your-jwt-secret
    networks:
      - app-network

  # ✅ 数据库服务
  postgres:
    image: postgres:15-alpine
    environment:
      - POSTGRES_DB=mydb
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=password
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - app-network

  # ✅ Redis 缓存
  redis:
    image: redis:7-alpine
    volumes:
      - redis_data:/data
    networks:
      - app-network

volumes:
  postgres_data:
  redis_data:

networks:
  app-network:
    driver: bridge
```

## 📝 .dockerignore 配置

### 忽略文件配置
```dockerignore
# .dockerignore
# ✅ 忽略不必要的文件
node_modules
npm-debug.log
.git
.gitignore
README.md
.env
.nyc_output
coverage
.vscode
.idea
*.log
dist
build
.cache
.temp
```

## 🎯 Docker 最佳实践

### 必需配置项
- ✅ 使用多阶段构建减少镜像大小
- ✅ 创建非 root 用户提高安全性
- ✅ 配置 .dockerignore 忽略不必要文件
- ✅ 使用健康检查确保服务可用性
- ✅ 合理使用缓存层提高构建速度

### 参考文件
- `Dockerfile` - 容器构建文件
- `docker-compose.yml` - 服务编排配置
- `nginx.conf` - Nginx 配置文件
- `.dockerignore` - 忽略文件配置