🐛 fix(deploy): backend 宿主机端口受 .env BACKEND_PORT 控制

上一版 backend 写死 expose: ["8000"](不暴露宿主),但这违背"全局服
务的最终映射到宿主机只能受 .env 中的端口配置决定"的原则——调试场景下
.env 没办法把 backend 暴露到宿主。

修正:backend ports 改为 "${BACKEND_PORT:-}:8000",由 .env 决定:
- BACKEND_PORT 留空/未设 = 不暴露宿主端口(仅经前端 /api 反代)
- BACKEND_PORT=10003 = 暴露宿主 10003(调试 / 压测用)

.env.example 同步加注释说明。

验证:模拟 .env 两种场景下,端口映射完全由 BACKEND_PORT 决定。

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
2026-09-26 20:58:10 +08:00
parent b934b737e8
commit aaf887507f
2 changed files with 8 additions and 4 deletions
+3
View File
@@ -11,6 +11,9 @@ FRONTEND_PORT=10003
MOLDINSIGHT_PORT=10003
# inventory-only 独立部署时使用
INVENTORY_PORT=10004
# unified 模式 backend 是否暴露宿主端口:留空 = 不暴露(仅经前端 /api 反代),
# 设值(如 10003)= 直接暴露(调试 / 压测用)
# BACKEND_PORT=
# ================================
DEBUG=false
+5 -4
View File
@@ -76,10 +76,11 @@ services:
image: gemold-backend:latest
container_name: gemold_backend
command: ["python", "-m", "uvicorn", "entrypoints.unified:app", "--host", "0.0.0.0", "--port", "8000"]
# 不映射端口到宿主机:backend 仅供 frontend 反代(docker 网络内 backend:8000 互通),
# 避免与 frontend 抢占宿主 10003。BACKEND_PORT 仍保留以便调试时临时开放。
expose:
- "8000"
# 宿主机端口由 .env 的 BACKEND_PORT 决定:空/不设则不暴露宿主机端口
# (仅经前端 /api 反代同域访问,避免与 frontend 抢占宿主 10003)。
# 调试时设 BACKEND_PORT=10003 即可独立访问。
ports:
- "${BACKEND_PORT:-}:8000"
environment:
<<: *base_env
ADMIN_USERNAME: ${ADMIN_USERNAME:-admin}