feat:增加docker

This commit is contained in:
Join
2025-11-12 13:31:49 +08:00
parent c9a8b743c1
commit 181069e60d
4 changed files with 244 additions and 21 deletions
@@ -0,0 +1,33 @@
param()
$ErrorActionPreference = "Stop"
Write-Host "🚀 启动独立前端 Docker 服务..."
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$WebUiRoot = (Resolve-Path (Join-Path $ScriptDir "..")).Path
try { docker info | Out-Null } catch { Write-Host "❌ Docker 未运行,请先启动 Docker"; exit 1 }
$useDockerCompose = $false
try { docker compose version | Out-Null; $useDockerCompose = $true } catch { try { docker-compose version | Out-Null } catch { Write-Host "❌ Docker Compose 不可用,请检查安装"; exit 1 } }
if (-not (Test-Path (Join-Path $ScriptDir ".env"))) { if (Test-Path (Join-Path $ScriptDir "env.example")) { Copy-Item (Join-Path $ScriptDir "env.example") (Join-Path $ScriptDir ".env") -Force } }
if (-not (Test-Path (Join-Path $WebUiRoot "dist"))) {
Write-Host "🔨 前端未构建,开始执行 NPM 构建..."
Push-Location $WebUiRoot
& npm ci
& npm run build
Pop-Location
}
Write-Host "🔧 构建镜像并启动服务..."
Push-Location $ScriptDir
if ($useDockerCompose) { docker compose up -d --build } else { docker-compose up -d --build }
Pop-Location
$port = if ($env:FRONTEND_PORT) { $env:FRONTEND_PORT } else { 80 }
Write-Host "✅ 前端已启动: http://localhost:$port"
Write-Host "📊 查看状态: (cd cn-universal-web-ui/docker && docker compose ps) 或 docker-compose ps"
Write-Host "🔍 查看日志: (cd cn-universal-web-ui/docker && docker compose logs -f) 或 docker-compose logs -f"
+114
View File
@@ -0,0 +1,114 @@
param(
[string]$Backend = "default",
[string]$Frontend = "default"
)
$ErrorActionPreference = "Stop"
Write-Host "🚀 开始构建 Docker 镜像 (JDK 21 + MySQL 5.8 + EMQX)..."
Write-Host ""
Write-Host "📋 支持的构建版本:"
Write-Host " 后端版本:"
Write-Host " .\build-docker.ps1 -Backend <default|alpine|corretto> -Frontend <default|ubuntu|centos|simple|local>"
Write-Host ""
Write-Host " 示例:"
Write-Host " .\build-docker.ps1"
Write-Host " .\build-docker.ps1 -Backend alpine"
Write-Host " .\build-docker.ps1 -Backend corretto -Frontend ubuntu"
Write-Host " .\build-docker.ps1 -Frontend simple"
Write-Host ""
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$RepoRoot = (Resolve-Path (Join-Path $ScriptDir "..")).Path
# .env
if (-not (Test-Path (Join-Path $ScriptDir ".env"))) {
Write-Host "📝 创建 .env 文件..."
Copy-Item (Join-Path $ScriptDir "env.example") (Join-Path $ScriptDir ".env") -Force
Write-Host "✅ .env 文件已创建,请根据需要修改配置"
}
# 构建后端
if (-not (Test-Path (Join-Path $RepoRoot "cn-universal-web/target/cn-universal-web"))) {
Write-Host "🔨 后端未构建,开始执行 Maven Reactor 构建..."
Push-Location $RepoRoot
& mvn -q -T 1C -DskipTests -pl cn-universal-web -am install
& mvn -q -DskipTests -pl cn-universal-web package
Pop-Location
}
# 构建前端
if (-not (Test-Path (Join-Path $RepoRoot "cn-universal-web-ui/dist"))) {
Write-Host "🔨 前端未构建,开始执行 NPM 构建..."
Push-Location (Join-Path $RepoRoot "cn-universal-web-ui")
& npm ci
& npm run build
Pop-Location
}
# 后端镜像
Write-Host "🔨 构建后端镜像..."
Push-Location (Join-Path $RepoRoot "cn-universal-web")
switch ($Backend) {
"alpine" {
Write-Host "📦 使用 Alpine 版本 (镜像更小)"
& docker build -f Dockerfile.alpine -t cn-universal-backend:alpine .
& docker tag cn-universal-backend:alpine cn-universal-backend:latest
}
"corretto" {
Write-Host "🏢 使用 Amazon Corretto 版本 (企业级支持)"
& docker build -f Dockerfile.amazoncorretto -t cn-universal-backend:corretto .
& docker tag cn-universal-backend:corretto cn-universal-backend:latest
}
Default {
Write-Host "📦 使用默认 OpenJDK 版本"
& docker build -t cn-universal-backend:latest .
}
}
Pop-Location
# 前端镜像
Write-Host "🔨 构建前端镜像..."
Push-Location (Join-Path $RepoRoot "cn-universal-web-ui")
switch ($Frontend) {
"ubuntu" {
Write-Host "🐧 使用 Ubuntu 版本"
& docker build -f Dockerfile.ubuntu -t cn-universal-frontend:ubuntu .
& docker tag cn-universal-frontend:ubuntu cn-universal-frontend:latest
}
"centos" {
Write-Host "🔴 使用 CentOS 版本"
& docker build -f Dockerfile.centos -t cn-universal-frontend:centos .
& docker tag cn-universal-frontend:centos cn-universal-frontend:latest
}
"simple" {
Write-Host "🐍 使用 Python 简单版本"
& docker build -f Dockerfile.simple -t cn-universal-frontend:simple .
& docker tag cn-universal-frontend:simple cn-universal-frontend:latest
}
"local" {
Write-Host "🏠 使用本地镜像版本"
& docker build -f Dockerfile.local -t cn-universal-frontend:local .
& docker tag cn-universal-frontend:local cn-universal-frontend:latest
}
Default {
Write-Host "📦 使用默认 Alpine 版本"
& docker build -t cn-universal-frontend:latest .
}
}
Pop-Location
Write-Host "✅ 镜像构建完成!"
Write-Host ""
Write-Host "📋 可用镜像:"
Write-Host " - cn-universal-backend:latest"
Write-Host " - cn-universal-frontend:latest"
Write-Host ""
Write-Host "🚀 启动服务:"
Write-Host " cd docker && docker-compose up -d"
Write-Host ""
Write-Host "📊 查看服务状态:"
Write-Host " cd docker && docker-compose ps"
Write-Host ""
Write-Host "🔍 查看日志:"
Write-Host " cd docker && docker-compose logs -f"
+26 -21
View File
@@ -1,6 +1,5 @@
#!/bin/bash
# Docker 构建脚本
set -e
echo "🚀 开始构建 Docker 镜像 (JDK 21 + MySQL 5.8 + EMQX)..."
@@ -18,29 +17,36 @@ echo " ./build-docker.sh corretto ubuntu # 后端 Corretto + 前端 Ubunt
echo " ./build-docker.sh default simple # 后端默认 + 前端 Python 简单版"
echo ""
# 检查必要文件
if [ ! -d "../cn-universal-web/target/cn-universal-web" ]; then
echo "❌ 后端构建文件不存在,请先执行 Maven 构建"
echo " 执行: mvn clean package -DskipTests"
exit 1
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
echo "🛠 检查并构建前后端产物..."
if [ ! -d "${REPO_ROOT}/cn-universal-web/target/cn-universal-web" ]; then
echo "🔨 后端未构建,开始执行 Maven Reactor 构建..."
(
cd "${REPO_ROOT}"
mvn -q -T 1C -DskipTests -pl cn-universal-web -am install
mvn -q -DskipTests -pl cn-universal-web package
)
fi
if [ ! -d "../cn-universal-web-ui/dist" ]; then
echo " 前端构建文件不存在,请先执行前端构建"
echo " 执行: cd ../cn-universal-web-ui && npm run build"
exit 1
if [ ! -d "${REPO_ROOT}/cn-universal-web-ui/dist" ]; then
echo "🔨 前端构建,开始执行 NPM 构建..."
(
cd "${REPO_ROOT}/cn-universal-web-ui"
npm ci
npm run build
)
fi
# 创建 .env 文件(如果不存在)
if [ ! -f ".env" ]; then
echo "📝 创建 .env 文件..."
cp env.example .env
echo "✅ .env 文件已创建,请根据需要修改配置"
if [ ! -f "${SCRIPT_DIR}/.env" ]; then
echo "📝 创建 .env 文件..."
cp "${SCRIPT_DIR}/env.example" "${SCRIPT_DIR}/.env"
echo "✅ .env 文件已创建,请根据需要修改配置"
fi
# 构建后端镜像
echo "🔨 构建后端镜像..."
cd ../cn-universal-web
cd "${REPO_ROOT}/cn-universal-web"
# 选择 Dockerfile 版本
DOCKERFILE_VERSION=${1:-default}
@@ -61,11 +67,10 @@ case $DOCKERFILE_VERSION in
;;
esac
cd ../docker
cd "${SCRIPT_DIR}"
# 构建前端镜像
echo "🔨 构建前端镜像..."
cd ../cn-universal-web-ui
cd "${REPO_ROOT}/cn-universal-web-ui"
# 选择前端 Dockerfile 版本
FRONTEND_VERSION=${2:-default}
@@ -96,7 +101,7 @@ case $FRONTEND_VERSION in
;;
esac
cd ..
cd "${REPO_ROOT}"
echo "✅ 镜像构建完成!"
echo ""
+71
View File
@@ -0,0 +1,71 @@
param()
$ErrorActionPreference = "Stop"
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$RepoRoot = (Resolve-Path (Join-Path $ScriptDir "..")).Path
Write-Host "🚀 启动 IoT 平台 Docker 服务..."
try { docker info | Out-Null } catch { Write-Host "❌ Docker 未运行,请先启动 Docker"; exit 1 }
$useDockerCompose = $false
try { docker compose version | Out-Null; $useDockerCompose = $true } catch { try { docker-compose version | Out-Null } catch { Write-Host "❌ Docker Compose 不可用,请检查安装"; exit 1 } }
New-Item -ItemType Directory -Force -Path (Join-Path $ScriptDir "logs") | Out-Null
New-Item -ItemType Directory -Force -Path (Join-Path $ScriptDir "mysql/conf") | Out-Null
New-Item -ItemType Directory -Force -Path (Join-Path $ScriptDir "mysql/init") | Out-Null
if (-not (Test-Path (Join-Path $ScriptDir ".env"))) { Copy-Item (Join-Path $ScriptDir "env.example") (Join-Path $ScriptDir ".env") -Force }
Write-Host "🛠 检查并构建前后端..."
if (-not (Test-Path (Join-Path $RepoRoot "cn-universal-web/target/cn-universal-web"))) {
Write-Host "🔨 后端未构建,开始执行 Maven Reactor 构建..."
Push-Location $RepoRoot
& mvn -q -T 1C -DskipTests -pl cn-universal-web -am install
& mvn -q -DskipTests -pl cn-universal-web package
Pop-Location
}
if (-not (Test-Path (Join-Path $RepoRoot "cn-universal-web-ui/dist"))) {
Write-Host "🔨 前端未构建,开始执行 NPM 构建..."
Push-Location (Join-Path $RepoRoot "cn-universal-web-ui")
& npm ci
& npm run build
Pop-Location
}
Write-Host "🔧 构建镜像并启动服务..."
$skipFrontend = $false
try { docker pull nginx:alpine | Out-Null } catch { Write-Host "⚠️ 无法拉取 nginx:alpine,将仅启动后端与基础服务"; $skipFrontend = $true }
Push-Location $ScriptDir
if ($useDockerCompose) {
if ($skipFrontend) { docker compose up -d --build backend mysql redis emqx adminer } else { docker compose up -d --build }
} else {
if ($skipFrontend) { docker-compose up -d --build backend mysql redis emqx adminer } else { docker-compose up -d --build }
}
Pop-Location
Start-Sleep -Seconds 10
Write-Host "📊 服务状态:"
if ($useDockerCompose) { Push-Location $ScriptDir; docker compose ps; Pop-Location } else { Push-Location $ScriptDir; docker-compose ps; Pop-Location }
Write-Host ""
Write-Host "🌐 服务访问地址:"
Write-Host " 前端界面: http://localhost:80"
Write-Host " 后端 API: http://localhost:9092"
Write-Host " EMQX 管理: http://localhost:18083 (admin/public)"
Write-Host " 数据库管理: http://localhost:8081"
if ($skipFrontend) { Write-Host "⚠️ 已跳过前端。后端接口: http://localhost:9092" }
Write-Host ""
Write-Host "📋 常用命令:"
Write-Host " 查看日志: (cd docker && docker compose logs -f) 或 docker-compose logs -f"
Write-Host " 停止服务: (cd docker && docker compose down) 或 docker-compose down"
Write-Host " 重启服务: (cd docker && docker compose restart) 或 docker-compose restart"
Write-Host " 查看状态: (cd docker && docker compose ps) 或 docker-compose ps"
Write-Host ""
Write-Host "✅ 服务启动完成!"