# deploy.ps1 - Spring Boot + Vue 项目 Docker 部署脚本 Write-Host "🚀 AIGC 平台 Docker 部署脚本" -ForegroundColor Green Write-Host ("=" * 60) # 检查必要文件 $requiredFiles = @("docker-compose.yml", "backend/Dockerfile", "frontend/Dockerfile", "frontend/nginx.conf") foreach ($file in $requiredFiles) { if (-not (Test-Path $file)) { Write-Host "❌ 缺少必要文件: $file" -ForegroundColor Red exit 1 } } Write-Host "✅ 所有必要文件检查通过" -ForegroundColor Green # 检查 Docker 环境 try { $dockerVersion = docker --version 2>&1 $composeVersion = docker-compose --version 2>&1 Write-Host "✅ Docker 环境: $dockerVersion" -ForegroundColor Green Write-Host "✅ Docker Compose: $composeVersion" -ForegroundColor Green } catch { Write-Host "❌ Docker 未正确安装或启动" -ForegroundColor Red Write-Host " 请确保已安装 Docker Desktop 并已启动" -ForegroundColor Yellow exit 1 } # 检查前端是否已构建 if (-not (Test-Path "frontend/dist")) { Write-Host "`n⚠️ 前端未构建,开始构建前端..." -ForegroundColor Yellow Push-Location frontend try { if (Test-Path "package.json") { Write-Host " 安装依赖..." -ForegroundColor Cyan npm install Write-Host " 构建生产版本..." -ForegroundColor Cyan npm run build Write-Host "✅ 前端构建完成" -ForegroundColor Green } else { Write-Host "⚠️ 未找到 package.json,跳过前端构建" -ForegroundColor Yellow } } catch { Write-Host "❌ 前端构建失败: $($_.Exception.Message)" -ForegroundColor Red Write-Host " 可以稍后手动构建: cd frontend && npm install && npm run build" -ForegroundColor Yellow } finally { Pop-Location } } else { Write-Host "✅ 前端已构建" -ForegroundColor Green } # 停止现有服务 Write-Host "`n🛑 停止现有服务..." -ForegroundColor Yellow docker-compose down 2>&1 | Out-Null # 构建镜像 Write-Host "`n📦 构建 Docker 镜像..." -ForegroundColor Cyan Write-Host " 这可能需要几分钟时间,请耐心等待..." -ForegroundColor Gray $buildResult = docker-compose build --no-cache 2>&1 if ($LASTEXITCODE -ne 0) { Write-Host "❌ 镜像构建失败" -ForegroundColor Red Write-Host $buildResult -ForegroundColor Red exit 1 } Write-Host "✅ 镜像构建完成" -ForegroundColor Green # 启动服务 Write-Host "`n🐳 启动所有服务..." -ForegroundColor Cyan docker-compose up -d if ($LASTEXITCODE -ne 0) { Write-Host "❌ 服务启动失败" -ForegroundColor Red exit 1 } # 等待服务启动 Write-Host "`n⏳ 等待服务启动(60秒)..." -ForegroundColor Yellow Write-Host " 后端服务需要时间连接数据库并初始化..." -ForegroundColor Gray Start-Sleep -Seconds 60 # 检查服务状态 Write-Host "`n🔍 检查服务状态..." -ForegroundColor Cyan docker-compose ps # 健康检查 Write-Host "`n🏥 服务健康检查..." -ForegroundColor Cyan # 检查 MySQL 服务 Write-Host " 检查 MySQL..." -ForegroundColor Gray $mysqlStatus = docker-compose ps mysql 2>&1 | Select-String "Up" if ($mysqlStatus) { Write-Host " ✅ MySQL 服务运行中" -ForegroundColor Green } else { Write-Host " ⚠️ MySQL 服务可能未正常启动" -ForegroundColor Yellow } # 检查 Redis 服务 Write-Host " 检查 Redis..." -ForegroundColor Gray $redisStatus = docker-compose ps redis 2>&1 | Select-String "Up" if ($redisStatus) { Write-Host " ✅ Redis 服务运行中" -ForegroundColor Green } else { Write-Host " ⚠️ Redis 服务可能未正常启动" -ForegroundColor Yellow } # 检查后端服务 Write-Host " 检查后端服务..." -ForegroundColor Gray try { # 检查后端服务是否响应 $backendTest = Invoke-WebRequest -Uri "http://localhost:8080" -TimeoutSec 5 -ErrorAction SilentlyContinue if ($backendTest.StatusCode -eq 200 -or $backendTest.StatusCode -eq 404 -or $backendTest.StatusCode -eq 302) { Write-Host " ✅ 后端服务可访问 (状态码: $($backendTest.StatusCode))" -ForegroundColor Green } } catch { # 尝试检查API端点 try { $apiTest = Invoke-WebRequest -Uri "http://localhost:8080/api" -TimeoutSec 5 -ErrorAction SilentlyContinue Write-Host " ✅ 后端API可访问" -ForegroundColor Green } catch { Write-Host " ⚠️ 后端服务可能还在启动中,请稍后检查" -ForegroundColor Yellow Write-Host " 查看日志: docker-compose logs backend" -ForegroundColor Gray Write-Host " 提示: 首次启动可能需要1-2分钟" -ForegroundColor Gray } } # 检查前端服务 Write-Host " 检查前端服务..." -ForegroundColor Gray try { $frontendResponse = Invoke-WebRequest -Uri "http://localhost" -TimeoutSec 5 -ErrorAction SilentlyContinue if ($frontendResponse.StatusCode -eq 200) { Write-Host " ✅ 前端服务运行正常" -ForegroundColor Green } } catch { Write-Host " ⚠️ 前端服务检查失败: $($_.Exception.Message)" -ForegroundColor Yellow Write-Host " 查看日志: docker-compose logs frontend" -ForegroundColor Gray } # 显示访问信息 Write-Host "`n" + ("=" * 60) -ForegroundColor Green Write-Host "🎉 部署完成!" -ForegroundColor Green Write-Host "" Write-Host "📍 访问地址:" -ForegroundColor Cyan Write-Host " 前端: http://localhost" -ForegroundColor White Write-Host " 后端API: http://localhost:8080" -ForegroundColor White Write-Host " API文档: http://localhost:8080/api" -ForegroundColor White Write-Host "" Write-Host "📍 数据库连接信息:" -ForegroundColor Cyan Write-Host " 地址: localhost:3306" -ForegroundColor White Write-Host " 用户: aigc_user" -ForegroundColor White Write-Host " 密码: aigc_password" -ForegroundColor White Write-Host " 数据库: aigc_platform" -ForegroundColor White Write-Host "" Write-Host "📍 Redis:" -ForegroundColor Cyan Write-Host " 地址: localhost:6379" -ForegroundColor White Write-Host "" Write-Host "📋 常用命令:" -ForegroundColor Yellow Write-Host " 查看所有服务日志: docker-compose logs -f" -ForegroundColor Gray Write-Host " 查看后端日志: docker-compose logs -f backend" -ForegroundColor Gray Write-Host " 查看前端日志: docker-compose logs -f frontend" -ForegroundColor Gray Write-Host " 查看服务状态: docker-compose ps" -ForegroundColor Gray Write-Host " 停止服务: docker-compose down" -ForegroundColor Gray Write-Host " 停止并删除数据: docker-compose down -v" -ForegroundColor Gray Write-Host " 重新部署: .\deploy.ps1" -ForegroundColor Gray Write-Host "" Write-Host "⚠️ 注意事项:" -ForegroundColor Yellow Write-Host " 1. 首次启动可能需要更长时间,请耐心等待" -ForegroundColor Gray Write-Host " 2. 如果服务未正常启动,请查看日志排查问题" -ForegroundColor Gray Write-Host " 3. 确保端口 80、8080、3306、6379 未被占用" -ForegroundColor Gray Write-Host ("=" * 60) -ForegroundColor Green