Files
wechatAiclaw/run-docker.sh
2026-03-11 11:17:46 +08:00

49 lines
1.5 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
# 生产部署脚本:启动前拉取最新代码 → 构建镜像 → 停止旧容器 → 启动新容器
set -e
IMAGE_NAME="wechat-admin-backend"
CONTAINER_NAME="wechat-admin-backend"
PORT="${PORT:-3000}"
# 数据目录挂载到宿主机防止容器删除后丢失SQLite 库 wechat.db 及表数据)
HOST_DATA_DIR="${HOST_DATA_DIR:-$(pwd)/data}"
# 启动前自动获取最新代码(与远端 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_FILE=".env"
if [ ! -f "${ENV_FILE}" ]; then
echo "Env file ${ENV_FILE} not found, copying from .env.example ..."
cp .env.example "${ENV_FILE}"
fi
echo "Running container ${CONTAINER_NAME} on port ${PORT}..."
docker run -d \
--name "${CONTAINER_NAME}" \
--env-file "${ENV_FILE}" \
-p "${PORT}:3000" \
-p "8000:8000" \
-v "${HOST_DATA_DIR}:/app/backend/data" \
"${IMAGE_NAME}"
echo "Container started. Data persisted on host: ${HOST_DATA_DIR}"
echo "Health check: curl http://localhost:${PORT}/health"