fix(deploy): run database seed from compiled dist in Docker

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-06-08 17:45:33 +08:00
parent 55a7043cbd
commit b4d3315db8
7 changed files with 35 additions and 12 deletions

View File

@@ -15,5 +15,5 @@ SEED_DATABASE=true
# 对外端口(宝塔/Nginx 反代时可改回 80/443 或保留以下端口) # 对外端口(宝塔/Nginx 反代时可改回 80/443 或保留以下端口)
API_PORT=3000 API_PORT=3000
PLAYER_PORT=8080 PLAYER_PORT=8082
ADMIN_PORT=8081 ADMIN_PORT=8081

View File

@@ -0,0 +1,11 @@
import { PrismaClient } from '@prisma/client';
import { runSeed } from './run-seed';
const prisma = new PrismaClient();
runSeed(prisma)
.catch((err) => {
console.error(err);
process.exit(1);
})
.finally(() => prisma.$disconnect());

View File

@@ -72,7 +72,7 @@ services:
depends_on: depends_on:
- api - api
ports: ports:
- '${PLAYER_PORT:-8080}:80' - '${PLAYER_PORT:-8082}:80'
restart: unless-stopped restart: unless-stopped
networks: networks:
- thebet365 - thebet365

View File

@@ -12,7 +12,7 @@ npx prisma generate
if [ "$SEED_DATABASE" = "true" ]; then if [ "$SEED_DATABASE" = "true" ]; then
echo "[api] seeding database..." echo "[api] seeding database..."
npx prisma db seed node dist/infrastructure/database/seed-cli.js
fi fi
if [ -d /app/uploads-default/banners ] && [ ! -f /app/uploads/banners/welcome.svg ]; then if [ -d /app/uploads-default/banners ] && [ ! -f /app/uploads/banners/welcome.svg ]; then

View File

@@ -10,7 +10,7 @@
```text ```text
浏览器 浏览器
├─ :8080 player (Nginx) ── /api、/uploads ──► api (NestJS :3000) ├─ :8082 player (Nginx) ── /api、/uploads ──► api (NestJS :3000)
└─ :8081 admin (Nginx) ── /api ────────────► api (NestJS :3000) └─ :8081 admin (Nginx) ── /api ────────────► api (NestJS :3000)
api ──► postgres:5432 api ──► postgres:5432
@@ -19,7 +19,7 @@ api ──► postgres:5432
| 容器 | 默认端口 | 说明 | | 容器 | 默认端口 | 说明 |
|------|----------|------| |------|----------|------|
| `thebet365-player` | 8080 | 玩家 H5 前台 | | `thebet365-player` | 8082 | 玩家 H5 前台 |
| `thebet365-admin` | 8081 | 管理后台(平台 + 代理) | | `thebet365-admin` | 8081 | 管理后台(平台 + 代理) |
| `thebet365-api` | 3000 | NestJS API / Swagger | | `thebet365-api` | 3000 | NestJS API / Swagger |
| `thebet365-postgres` | 不对外暴露 | PostgreSQL 16 | | `thebet365-postgres` | 不对外暴露 | PostgreSQL 16 |
@@ -84,7 +84,7 @@ docker compose -f docker-compose.prod.yml --env-file .env.docker logs -f api
| 服务 | 地址 | | 服务 | 地址 |
|------|------| |------|------|
| 玩家前台 | http://服务器IP:8080 | | 玩家前台 | http://服务器IP:8082 |
| 管理后台 | http://服务器IP:8081 | | 管理后台 | http://服务器IP:8081 |
| API 文档 | http://服务器IP:3000/api/docs | | API 文档 | http://服务器IP:3000/api/docs |
@@ -111,7 +111,7 @@ docker compose -f docker-compose.prod.yml --env-file .env.docker logs -f api
```nginx ```nginx
location / { location / {
proxy_pass http://127.0.0.1:8080; proxy_pass http://127.0.0.1:8082;
proxy_set_header Host $host; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
@@ -119,7 +119,7 @@ location / {
} }
``` ```
**管理站** — 将 `8080` 改为 `8081` **管理站** — 将 `8082` 改为 `8081`
API 端口 3000 建议**不要**直接公网暴露Swagger 仅供内网或通过 VPN 访问。 API 端口 3000 建议**不要**直接公网暴露Swagger 仅供内网或通过 VPN 访问。
@@ -133,7 +133,7 @@ API 端口 3000 建议**不要**直接公网暴露Swagger 仅供内网或通
|------|--------|------|------| |------|--------|------|------|
| 平台管理员 | admin | Admin@123 | 管理后台 :8081 | | 平台管理员 | admin | Admin@123 | 管理后台 :8081 |
| 一级代理 | agent1 | Agent@123 | 管理后台 :8081 | | 一级代理 | agent1 | Agent@123 | 管理后台 :8081 |
| 玩家 | player1 | Player@123 | 玩家前台 :8080 | | 玩家 | player1 | Player@123 | 玩家前台 :8082 |
--- ---

View File

@@ -310,7 +310,7 @@ docker compose -f docker-compose.prod.yml --env-file .env.docker ps
| 应用 | 地址 | | 应用 | 地址 |
|------|------| |------|------|
| 玩家前台 | `http://你的IP:8080` | | 玩家前台 | `http://你的IP:8082` |
| 管理后台 | `http://你的IP:8081` | | 管理后台 | `http://你的IP:8081` |
| API / Swagger | `http://你的IP:3000/api/docs` | | API / Swagger | `http://你的IP:3000/api/docs` |
@@ -414,7 +414,7 @@ docker compose -f docker-compose.prod.yml --env-file .env.docker logs --tail=30
docker compose -f docker-compose.prod.yml --env-file .env.docker logs --tail=30 api docker compose -f docker-compose.prod.yml --env-file .env.docker logs --tail=30 api
# 本机探测(在服务器上) # 本机探测(在服务器上)
curl -I http://127.0.0.1:8080 curl -I http://127.0.0.1:8082
curl -I http://127.0.0.1:3000/api/docs curl -I http://127.0.0.1:3000/api/docs
``` ```
@@ -528,6 +528,18 @@ docker compose -f docker-compose.prod.yml --env-file .env.docker up -d
- 那是**宿主机上其他项目**(彩票系统)的数据,与 thebet365 Docker 内 `thebet365-redis` **不是同一个实例** - 那是**宿主机上其他项目**(彩票系统)的数据,与 thebet365 Docker 内 `thebet365-redis` **不是同一个实例**
- 查看本项目 Redis`docker exec -it thebet365-redis redis-cli KEYS '*'` - 查看本项目 Redis`docker exec -it thebet365-redis redis-cli KEYS '*'`
### 12. player 启动失败 `8080: bind: address already in use`
- 宝塔/同机其他项目常占用 8080本项目默认玩家端口已改为 **8082**
- 服务器 `.env.docker` 设置 `PLAYER_PORT=8082`,并放行安全组/防火墙该端口
- 重启:`docker compose -f docker-compose.prod.yml --env-file .env.docker up -d player`
### 13. API 反复重启,`seed.ts` / `run-seed` 找不到
- 原因:生产镜像只有编译后的 `dist``prisma db seed``ts-node` 需要 `src/` 源码
- 已改为 entrypoint 执行 `node dist/infrastructure/database/seed-cli.js`;拉代码后重建 api 镜像
- **临时绕过**(先让 API 起来):`.env.docker``SEED_DATABASE=false``up -d api`
--- ---
## 十、推荐日常开发顺序 ## 十、推荐日常开发顺序

View File

@@ -12,7 +12,7 @@ docker compose -f docker-compose.prod.yml --env-file .env.docker up -d --build
echo "" echo ""
echo "TheBet365 stack is starting." echo "TheBet365 stack is starting."
echo " Player: http://localhost:${PLAYER_PORT:-8080}" echo " Player: http://localhost:${PLAYER_PORT:-8082}"
echo " Admin: http://localhost:${ADMIN_PORT:-8081}" echo " Admin: http://localhost:${ADMIN_PORT:-8081}"
echo " API: http://localhost:${API_PORT:-3000}/api/docs" echo " API: http://localhost:${API_PORT:-3000}/api/docs"
echo "" echo ""