Files
wechatAiclaw/run-docker.sh
丹尼尔 bdba4ec071 fix
2026-03-12 11:52:04 +08:00

69 lines
2.7 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 生产部署脚本:启动前拉取最新代码 → 构建镜像 → 停止旧容器 → 启动新容器
# 用法: ./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
set -e
IMAGE_NAME="wechat-admin-backend"
CONTAINER_NAME="wechat-admin-backend"
PORT="${PORT:-3000}"
BACKEND_PORT="${BACKEND_PORT:-8000}"
HOST_DATA_DIR="${HOST_DATA_DIR:-$(pwd)/data}"
while [[ $# -gt 0 ]]; do
case $1 in
-p|--port) PORT="$2"; shift 2 ;;
-b|--backend-port) BACKEND_PORT="$2"; shift 2 ;;
-d|--data-dir) HOST_DATA_DIR="$2"; shift 2 ;;
-h|--help) echo "Usage: $0 [-p PORT] [-b BACKEND_PORT] [-d HOST_DATA_DIR]"; echo " -p, --port PORT frontend (default 3000)"; echo " -b, --backend-port PORT backend API (default 8000)"; echo " -d, --data-dir DIR data volume (default \$(pwd)/data)"; exit 0 ;;
*) echo "Unknown option: $1 (use -h for help)"; exit 1 ;;
esac
done
# 启动前自动获取最新代码(与远端 master 一致,丢弃本地修改)
if git rev-parse --git-dir >/dev/null 2>&1; then
echo "Fetching and reset to origin/master..."
git fetch --all && git reset --hard origin/master
else
echo "Not a git repo, skip git fetch/reset."
fi
echo "Building Docker image: ${IMAGE_NAME}..."
docker build -t "${IMAGE_NAME}" .
echo "Stopping and removing existing container (if any)..."
if [ "$(docker ps -aq -f name=${CONTAINER_NAME})" ]; then
docker rm -f "${CONTAINER_NAME}" >/dev/null 2>&1 || true
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}"
fi
fi
echo "Running container ${CONTAINER_NAME} (frontend :${PORT}, backend :${BACKEND_PORT})..."
docker run -d \
--name "${CONTAINER_NAME}" \
--env-file "${ENV_FILE}" \
-p "${PORT}:3000" \
-p "${BACKEND_PORT}:8000" \
-v "${HOST_DATA_DIR}:/app/backend/data" \
"${IMAGE_NAME}"
echo "Container started. Data persisted on host: ${HOST_DATA_DIR}"
echo "Frontend: http://localhost:${PORT} | Backend API: http://localhost:${BACKEND_PORT} | Health: curl http://localhost:${PORT}/health"