# Docker 部署指南 Riwa App 分发页使用预构建模式部署,无需在服务器上安装依赖,快速启动。 ## 📋 前置要求 - 本地环境: Node.js 20+, pnpm - 服务器环境: Docker, Docker Compose ## 🚀 快速部署 ### 1. 本地构建 ```bash # 安装依赖 pnpm install # 构建生产版本 pnpm run build # 或使用部署脚本 ./deploy.sh ``` 构建完成后会生成 `.output/` 目录。 ### 2. 上传到服务器 #### 方式一:上传整个项目(推荐) ```bash # 使用 rsync 上传(排除 node_modules) rsync -avz --exclude 'node_modules' --exclude '.nuxt' --exclude '.git' \ ./ user@your-server:/path/to/riwa-distribute/ # 或使用 scp scp -r .output Dockerfile docker-compose.yml .dockerignore \ user@your-server:/path/to/riwa-distribute/ ``` #### 方式二:使用 Git(推荐用于生产环境) ```bash # 服务器上 git clone your-repo cd riwa-distribute # 本地构建后,将 .output 目录上传 rsync -avz .output/ user@your-server:/path/to/riwa-distribute/.output/ ``` ### 3. 服务器上启动 ```bash # SSH 登录到服务器 ssh user@your-server # 进入项目目录 cd /path/to/riwa-distribute/ # 启动容器 docker-compose up -d # 查看日志 docker-compose logs -f # 查看运行状态 docker-compose ps ``` ## 📊 访问应用 - 应用地址: http://your-server:3000 - API 健康检查: http://your-server:3000/api/version ## 🔧 常用命令 ```bash # 查看日志 docker-compose logs -f app # 重启服务 docker-compose restart app # 停止服务 docker-compose down # 更新部署(本地构建后) ./deploy.sh rsync -avz .output/ user@your-server:/path/to/riwa-distribute/.output/ docker-compose restart app ``` ## 🎯 生产环境建议 ### 使用 Nginx 反向代理 ```nginx server { listen 80; server_name distribute.riwa.com; location / { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; 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; } } ``` ### 配置 SSL ```bash # 使用 Let's Encrypt sudo certbot --nginx -d distribute.riwa.com ``` ### 环境变量配置 在服务器上创建 `.env` 文件: ```env NODE_ENV=production HOST=0.0.0.0 PORT=3000 ``` 修改 `docker-compose.yml` 加载环境变量: ```yaml services: app: env_file: - .env ``` ## 🔄 更新部署流程 ```bash # 1. 本地拉取最新代码 git pull origin main # 2. 本地构建 pnpm run build # 3. 上传构建产物 rsync -avz .output/ user@your-server:/path/to/riwa-distribute/.output/ # 4. 服务器重启 ssh user@your-server "cd /path/to/riwa-distribute && docker-compose restart app" ``` ## 📈 监控和日志 ```bash # 实时日志 docker-compose logs -f app # 查看最近 100 行日志 docker-compose logs --tail=100 app # 查看容器状态 docker stats riwa-distribute # 健康检查 curl http://localhost:3000/api/version ``` ## 🐛 故障排查 ### 容器无法启动 ```bash # 查看详细日志 docker-compose logs app # 检查构建产物 ls -la .output/ # 重新构建镜像 docker-compose build --no-cache app ``` ### 端口被占用 修改 `docker-compose.yml` 中的端口映射: ```yaml ports: - "8080:3000" # 改为其他端口 ``` ## 🔐 安全建议 1. 使用非 root 用户运行容器 2. 配置防火墙规则 3. 启用 HTTPS 4. 定期更新 Docker 镜像 5. 使用环境变量管理敏感信息 ## 📦 最小部署文件 如果只想上传最少文件,只需要: ``` .output/ # 构建产物(必需) Dockerfile # Docker 配置(必需) docker-compose.yml # Docker Compose 配置(必需) .dockerignore # 可选 ```