部署优化

This commit is contained in:
wchino
2026-06-13 22:16:14 +08:00
parent 21dd9957f0
commit 73a94e6be3
28 changed files with 899 additions and 129 deletions

View File

@@ -15,7 +15,7 @@
| `docs/docker/build-and-export-images.ps1` | WindowsPowerShell |
| `docs/docker/build-and-export-images.sh` | Linux / macOS / Git Bash |
两个脚本行为一致:在**项目根目录**执行 compose 构建 → 导出为根目录下的 `thebet365-images.tar`(默认)。
两个脚本行为一致:在**项目根目录**执行 compose 构建 → 导出为根目录下的 `thebet365-images-<tag>.tar`(默认),并生成同名 `.manifest.txt`
---
@@ -30,6 +30,7 @@
生产环境务必在 `.env.docker` 中配置:
- `POSTGRES_PASSWORD``JWT_SECRET`
- `IMAGE_TAG``BIND_ADDR``RUN_MIGRATIONS_ON_START` 保持 `.env.docker.example` 默认即可,部署脚本会按 `--tag` 写回真实版本
- `CHUANGLAN_ACCOUNT``CHUANGLAN_PASSWORD`(短信注册)
- `SEED_DATABASE=false`(生产建议保持 false由部署脚本按需一次性 seed
@@ -58,9 +59,10 @@ chmod +x docs/docker/build-and-export-images.sh
| PowerShell | Bash | 说明 |
|------------|------|------|
| `-Tag v1.2.3` | `--tag v1.2.3` | 指定镜像 tag未指定时默认当前 Git 短提交号 |
| (默认) | (默认) | `--no-cache` 全量构建,适合发版 |
| `-UseCache` | `--use-cache` | 使用 Docker 缓存,构建更快 |
| `-ExportOnly` | `--export-only` | 跳过构建,仅导出已有 `latest` 镜像 |
| `-ExportOnly` | `--export-only` | 跳过构建,仅导出已有指定 tag 镜像 |
| `-Output my.tar` | `--output my.tar` | 自定义导出文件名 |
示例:仅重新导出已有镜像
@@ -79,17 +81,18 @@ chmod +x docs/docker/build-and-export-images.sh
| 镜像名 | 说明 |
|--------|------|
| `thebet365-api:latest` | NestJS API含 Prisma 迁移入口 |
| `thebet365-player:latest` | 玩家前台Nginx 静态资源) |
| `thebet365-admin:latest` | 管理后台Nginx 静态资源) |
| `thebet365-api:<tag>` | NestJS API迁移由部署脚本执行 |
| `thebet365-player:<tag>` | 玩家前台Nginx 静态资源) |
| `thebet365-admin:<tag>` | 管理后台Nginx 静态资源) |
导出文件默认路径:
```text
<项目根目录>/thebet365-images.tar
<项目根目录>/thebet365-images-<tag>.tar
<项目根目录>/thebet365-images-<tag>.manifest.txt
```
文件已加入 `.gitignore`**请勿提交到 Git**。
这些文件已加入 `.gitignore`**请勿提交到 Git**。manifest 会记录 tag、构建时间、Git commit、镜像 ID 和 tar 的 SHA-256便于服务器核对发布包。
---
@@ -99,7 +102,8 @@ chmod +x docs/docker/build-and-export-images.sh
将以下内容传到服务器同一目录(如 `/www/wwwroot/thebet365`
- `thebet365-images.tar`
- `thebet365-images-<tag>.tar`
- `thebet365-images-<tag>.manifest.txt`
- `docker-compose.prod.yml`
- `.env.docker`(或服务器上已有配置)
- `docker/nginx/` 等 compose 依赖目录(若仅 load 镜像、不 rebuildcompose 文件仍需要)
@@ -111,16 +115,16 @@ chmod +x docs/docker/build-and-export-images.sh
```bash
cd /www/wwwroot/thebet365
chmod +x scripts/*.sh
./scripts/deploy-first.sh --images thebet365-images.tar
./scripts/deploy-first.sh --images thebet365-images-v1.2.3.tar --tag v1.2.3
```
后续更新同一个服务器时:
```bash
./scripts/deploy-update.sh --images thebet365-images.tar
./scripts/deploy-update.sh --images thebet365-images-v1.2.3.tar --tag v1.2.3
```
更新脚本会先备份数据库,再用新 API 镜像执行 `prisma migrate deploy`,最后替换运行中的容器。
更新脚本会先备份数据库与 uploads,再用新 API 镜像执行 `prisma migrate deploy`,最后替换运行中的容器并等待健康检查通过
### 3. 验证
@@ -131,8 +135,8 @@ docker logs thebet365-api --tail 50
浏览器访问(端口以 `.env.docker` 为准):
- 玩家端:`http://服务器IP:8082`
- 管理端:`http://服务器IP:8081`
- 玩家端:经宝塔反代访问,或服务器本机 `http://127.0.0.1:8082`
- 管理端:经宝塔反代访问,或服务器本机 `http://127.0.0.1:8081`
---
@@ -140,10 +144,10 @@ docker logs thebet365-api --tail 50
| 方式 | 优点 | 缺点 |
|------|------|------|
| **本地 build + 导出 tar** | 不占用服务器 CPU/内存;可重复部署同一包 | 需上传较大 tar约 200300 MB |
| **本地 build + 导出 tar** | 不占用服务器 CPU/内存;有 tag 与 manifest可重复部署同一包 | 需上传较大 tar约 200300 MB |
| **服务器 `docker compose build`** | 无需传 tar | 首次/全量构建慢,小内存机器易失败 |
发版推荐流程:**本地或构建机执行脚本 → 上传 tar → 服务器执行 `deploy-update.sh --images thebet365-images.tar`**。
发版推荐流程:**本地或构建机执行脚本 → 上传 tar + manifest → 服务器执行 `deploy-update.sh --images thebet365-images-<tag>.tar --tag <tag>`**。
---
@@ -162,9 +166,9 @@ find packages/shared/public -mindepth 1 -maxdepth 1 -type d \
! -name flags ! -name players -exec rm -rf {} +
```
### 3. `docker load` 后 `up -d` 仍拉取或重建镜像
### 3. `docker load` 后部署仍找不到镜像
确保 compose 中 `image` 与 load 的 tag 一致(`thebet365-api:latest` 等),且使用同一 `docker-compose.prod.yml`
确保上传的 tar 中包含 `thebet365-api:<tag>``thebet365-player:<tag>``thebet365-admin:<tag>`,并且服务器执行部署时传入同一 `--tag <tag>`
### 4. API 启动后不断重启
@@ -182,7 +186,8 @@ docker logs thebet365-api
thebet365/
├── docker-compose.prod.yml
├── .env.docker.example
├── thebet365-images.tar # 导出产物(默认,已 gitignore
├── thebet365-images-<tag>.tar # 导出产物(默认,已 gitignore
├── thebet365-images-<tag>.manifest.txt
├── docker/
│ ├── api/Dockerfile
│ ├── player/Dockerfile