This commit is contained in:
丹尼尔
2026-03-15 17:32:10 +08:00
parent 15c9e1772a
commit 19d8ad3721
7 changed files with 90 additions and 10 deletions

View File

@@ -1,10 +1,11 @@
#!/usr/bin/env bash
# 生产部署脚本:启动前拉取最新代码 → 构建镜像 → 停止旧容器 → 启动新容器
# 环境:仅读取 .env.prod不读 .env。首次部署请 cp .env.prod.example .env.prod 并编辑后执行本脚本。
# 用法: ./run-docker.sh [-p PORT] [-b BACKEND_PORT] [-d HOST_DATA_DIR]
# -p, --port PORT 前端宿主机端口,默认 3000
# -b, --backend-port PORT 后端 API 宿主机端口,默认 8000容器内固定 8000
# -d, --data-dir DIR 数据目录挂载,默认 ./data
cp .env.prod.example .env.prod
set -e
IMAGE_NAME="wechat-admin-backend"
@@ -42,17 +43,20 @@ fi
mkdir -p "${HOST_DATA_DIR}"
echo "Data dir (host): ${HOST_DATA_DIR} -> container /app/backend/data"
# 优先使用 .env.prod 作为生产环境配置(例如在服务器上单独维护 CALLBACK_BASE_URL 等),
# 若不存在则回退到 .env再没有则从 .env.example 复制一份。
if [ -f ".env.prod" ]; then
ENV_FILE=".env.prod"
else
ENV_FILE=".env"
if [ ! -f "${ENV_FILE}" ]; then
echo "Env file ${ENV_FILE} not found, copying from .env.example ..."
cp .env.example "${ENV_FILE}"
# 生产仅读取 .env.prod(不读 .env避免混用本地配置
ENV_FILE=".env.prod"
if [ ! -f "${ENV_FILE}" ]; then
if [ -f ".env.prod.example" ]; then
echo "Creating ${ENV_FILE} from .env.prod.example ..."
cp .env.prod.example "${ENV_FILE}"
echo "Please edit ${ENV_FILE} with production values (e.g. CALLBACK_BASE_URL, API keys) and run $0 again."
exit 1
else
echo "Error: ${ENV_FILE} and .env.prod.example not found. Cannot start production container."
exit 1
fi
fi
echo "Using env file: ${ENV_FILE}"
echo "Running container ${CONTAINER_NAME} (frontend :${PORT}, backend :${BACKEND_PORT})..."
docker run -d \