#!/bin/bash # =================================================================== # TopFans Docker 一键启动脚本 # 功能:构建并启动所有后端服务 # =================================================================== # # 使用方式: # ./start.sh # 构建并启动所有服务(本地) # ./start.sh --profile prod # 使用生产配置 # ./start.sh --build-only # 仅构建,不启动 # ./start.sh --no-cache # 无缓存构建 # # 示例: # ./start.sh # 本地开发模式 # ./start.sh --profile prod # 生产部署模式 # =================================================================== set -e # ==================== 颜色定义 ==================== RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m' # ==================== 路径配置 ==================== SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "$SCRIPT_DIR" # ==================== 默认设置 ==================== PROFILE="local" BUILD_ONLY=false NO_CACHE="" # ==================== 解析参数 ==================== while [[ $# -gt 0 ]]; do case $1 in --profile) PROFILE="$2" shift 2 ;; --build-only) BUILD_ONLY=true shift ;; --no-cache) NO_CACHE="--no-cache" shift ;; --help|-h) echo "用法: $0 [选项]" echo "" echo "选项:" echo " --profile 配置文件 (local/prod),默认 local" echo " --build-only 仅构建,不启动服务" echo " --no-cache 无缓存构建" echo " --help, -h 显示帮助" exit 0 ;; *) echo -e "${RED}错误: 未知选项 '$1'${NC}" exit 1 ;; esac done # ==================== 配置检查 ==================== COMPOSE_FILE="docker-compose.${PROFILE}.yml" ENV_FILE=".env.${PROFILE}" if [ ! -f "$COMPOSE_FILE" ]; then echo -e "${RED}错误: 配置文件 $COMPOSE_FILE 不存在${NC}" exit 1 fi if [ ! -f "$ENV_FILE" ]; then echo -e "${YELLOW}警告: $ENV_FILE 不存在,复制 .env.local 作为模板${NC}" if [ -f ".env.local" ]; then cp .env.local "$ENV_FILE" else echo -e "${RED}错误: 缺少环境配置文件${NC}" exit 1 fi fi # ==================== 打印信息 ==================== print_step() { echo "" echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}" echo -e "${BLUE} $1${NC}" echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}" } # ==================== 停止现有容器 ==================== print_step "🛑 停止现有容器" # 停止并移除旧容器 docker-compose -f "$COMPOSE_FILE" down 2>/dev/null || true echo -e "${GREEN}✅ 已停止旧容器${NC}" # ==================== 构建镜像 ==================== print_step "🔨 构建 Docker 镜像" # 调用构建脚本 if [ -n "$NO_CACHE" ]; then ./build.sh --no-cache else ./build.sh fi if [ $? -ne 0 ]; then echo -e "${RED}❌ 构建失败${NC}" exit 1 fi echo -e "${GREEN}✅ 镜像构建完成${NC}" # 如果仅构建则退出 if [ "$BUILD_ONLY" = true ]; then echo "" echo -e "${GREEN}🎉 构建完成(未启动服务)${NC}" exit 0 fi # ==================== 启动服务 ==================== print_step "🚀 启动服务" echo -e "${YELLOW}使用配置: ${PROFILE}${NC}" echo -e "${YELLOW}配置文件: ${COMPOSE_FILE}${NC}" echo "" # 启动所有服务(-d 后台运行) docker-compose -f "$COMPOSE_FILE" --profile ${PROFILE} up -d # ==================== 等待健康检查 ==================== print_step "⏳ 等待服务就绪" # 等待时间(秒) WAIT_TIME=60 INTERVAL=5 ELAPSED=0 echo -e "${YELLOW}正在等待服务启动...${NC}" while [ $ELAPSED -lt $WAIT_TIME ]; do # 检查 Gateway 健康状态 if curl -s http://localhost:8080/health > /dev/null 2>&1; then echo -e "${GREEN}✅ Gateway 就绪${NC}" break fi sleep $INTERVAL ELAPSED=$((ELAPSED + INTERVAL)) echo -e "${YELLOW} 已等待 ${ELAPSED}s...${NC}" done if [ $ELAPSED -ge $WAIT_TIME ]; then echo -e "${RED}⚠️ 服务启动超时,请检查日志${NC}" echo "查看日志: docker-compose -f ${COMPOSE_FILE} logs" fi # ==================== 显示结果 ==================== print_step "📊 服务状态" docker-compose -f "$COMPOSE_FILE" ps echo "" print_step "🌐 服务地址" echo "" echo -e " ${CYAN}Gateway:${NC} http://localhost:8080" echo -e " ${CYAN}Swagger UI:${NC} http://localhost:8080/swagger/index.html" echo -e " ${CYAN}PostgreSQL:${NC} localhost:5432" echo "" echo -e "${GREEN}🎉 启动完成!${NC}" echo "" echo "常用命令:" echo " 查看日志: docker-compose -f ${COMPOSE_FILE} logs -f" echo " 查看状态: docker-compose -f ${COMPOSE_FILE} ps" echo " 停止服务: docker-compose -f ${COMPOSE_FILE} down" echo ""