# Docker 部署指南

## 快速开始

### 1. 构建镜像
```bash
cd LQAdminPlatform
docker build -t lqadmin-platform .
```

### 2. 运行容器
```bash
docker run -d \
  --name lqadmin \
  -p 8000:8000 \
  -v $(pwd)/src/app/config/.env:/app/src/app/config/.env \
  lqadmin-platform
```

### 3. 验证部署
```bash
# 检查容器状态
docker ps

# 检查日志
docker logs lqadmin

# 测试 API
curl http://localhost:8000/health
```

## 配置说明

### 环境变量配置
可以通过以下方式配置：

1. **挂载配置文件**（推荐）
```bash
docker run -v /path/to/.env:/app/src/app/config/.env lqadmin-platform
```

2. **环境变量传递**
```bash
docker run \
  -e APP_NAME="SSO认证中心" \
  -e DATABASE_URL="mysql://user:pass@host:port/db" \
  -e JWT_SECRET_KEY="your-secret-key" \
  lqadmin-platform
```

### Docker Compose 部署

创建 `docker-compose.yml`：
```yaml
version: '3.8'

services:
  lqadmin:
    build: .
    ports:
      - "8000:8000"
    volumes:
      - ./src/app/config/.env:/app/src/app/config/.env
      - ./logs:/app/logs
    environment:
      - PYTHONPATH=/app:/app/src
    depends_on:
      - mysql
      - redis
    restart: unless-stopped

  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: admin
      MYSQL_DATABASE: lq_db
    ports:
      - "3306:3306"
    volumes:
      - mysql_data:/var/lib/mysql

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data

volumes:
  mysql_data:
  redis_data:
```

启动服务：
```bash
docker-compose up -d
```

## 生产环境配置

### 1. 安全配置
```bash
# 使用强密钥
JWT_SECRET_KEY=your-very-strong-secret-key-here
SECRET_KEY=your-app-secret-key-here

# 关闭调试模式
DEBUG=False

# 配置数据库
DATABASE_URL=mysql+aiomysql://user:password@mysql:3306/lq_db
```

### 2. 性能优化
```yaml
# docker-compose.yml
services:
  lqadmin:
    deploy:
      resources:
        limits:
          cpus: '2'
          memory: 2G
        reservations:
          cpus: '1'
          memory: 1G
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
```

### 3. 日志管理
```yaml
services:
  lqadmin:
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
```

## 故障排查

### 1. 容器无法启动
```bash
# 查看详细日志
docker logs lqadmin --details

# 进入容器调试
docker exec -it lqadmin /bin/bash
```

### 2. 模块导入错误
```bash
# 检查 Python 路径
docker exec lqadmin python -c "import sys; print('\n'.join(sys.path))"

# 检查文件结构
docker exec lqadmin find /app -name "*.py" | head -20
```

### 3. 配置文件问题
```bash
# 检查配置文件
docker exec lqadmin cat /app/src/app/config/.env

# 检查环境变量
docker exec lqadmin env | grep -E "(APP_|DATABASE_|JWT_)"
```

## 监控和维护

### 1. 健康检查
```bash
# API 健康检查
curl http://localhost:8000/health

# 容器健康状态
docker inspect lqadmin | grep Health -A 10
```

### 2. 性能监控
```bash
# 容器资源使用
docker stats lqadmin

# 应用日志
docker logs -f lqadmin
```

### 3. 备份和恢复
```bash
# 备份数据库
docker exec mysql mysqldump -u root -p lq_db > backup.sql

# 备份配置
cp src/app/config/.env backup/.env.$(date +%Y%m%d)
```

## 更新部署

### 1. 滚动更新
```bash
# 构建新镜像
docker build -t lqadmin-platform:v2 .

# 停止旧容器
docker stop lqadmin

# 启动新容器
docker run -d --name lqadmin-v2 -p 8000:8000 lqadmin-platform:v2

# 验证后删除旧容器
docker rm lqadmin
docker rename lqadmin-v2 lqadmin
```

### 2. 使用 Docker Compose
```bash
# 更新服务
docker-compose pull
docker-compose up -d --no-deps lqadmin
```

## 扩展部署

### 1. 负载均衡
```yaml
# nginx.conf
upstream lqadmin {
    server lqadmin1:8000;
    server lqadmin2:8000;
}

server {
    listen 80;
    location / {
        proxy_pass http://lqadmin;
    }
}
```

### 2. 多实例部署
```yaml
# docker-compose.yml
services:
  lqadmin:
    deploy:
      replicas: 3
    ports:
      - "8000-8002:8000"
```

这个部署指南涵盖了从开发到生产的完整 Docker 部署流程。