chore: Add Docker deployment config for admin-web

Add complete deployment toolchain for the Genex Admin Web (React + Next.js),
mirroring the proven deployment pattern from the rwadurian project.

## Files added

### deploy.sh — One-click deployment script
- Commands: build / start / stop / restart / logs / status / clean
- Auto-detects `docker compose` vs `docker-compose`
- Port conflict detection with auto-stop of old service
- Health check verification after startup
- Supports PORT env override (default: 3000)

### Dockerfile — Multi-stage production build
- Stage 1 (deps): node:20-alpine, npm ci for deterministic installs
- Stage 2 (builder): Next.js production build with telemetry disabled
- Stage 3 (runner): Minimal runtime with standalone output
- Non-root user (nextjs:nodejs) for security
- curl installed for container health checks

### docker-compose.yml — Container orchestration
- Service: genex-admin-web on genex-network bridge
- Health check: GET /api/health every 30s, 3 retries
- Auto-restart: unless-stopped policy
- Timezone: Asia/Shanghai

### .env.development — Local dev environment
- API: http://localhost:8080/api

### .env.production — Production environment
- API: https://api.gogenex.com/api

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-02-11 01:22:22 -08:00
parent e450bef7cd
commit bae342f1bd
5 changed files with 312 additions and 0 deletions

View File

@ -0,0 +1,6 @@
# API Configuration
NEXT_PUBLIC_API_BASE_URL=http://localhost:8080/api
# Application Info
NEXT_PUBLIC_APP_NAME=Genex Admin
NEXT_PUBLIC_VERSION=1.0.0-dev

View File

@ -0,0 +1,6 @@
# API Configuration
NEXT_PUBLIC_API_BASE_URL=https://api.gogenex.com/api
# Application Info
NEXT_PUBLIC_APP_NAME=Genex Admin
NEXT_PUBLIC_VERSION=1.0.0

View File

@ -0,0 +1,56 @@
# 阶段1: 依赖安装
FROM node:20-alpine AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
# 复制依赖文件
COPY package.json package-lock.json ./
# 安装依赖
RUN npm ci --only=production=false
# 阶段2: 构建
FROM node:20-alpine AS builder
WORKDIR /app
# 复制依赖
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# 设置环境变量
ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=production
# 构建应用
RUN npm run build
# 阶段3: 生产运行
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
# 安装 curl 用于健康检查
RUN apk add --no-cache curl
# 创建非 root 用户
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# 复制构建产物
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
# 设置权限
USER nextjs
# 暴露端口
EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
# 启动应用
CMD ["node", "server.js"]

View File

@ -0,0 +1,218 @@
#!/bin/bash
# Genex Admin Web 一键部署脚本
# 使用方法: ./deploy.sh [命令]
# 命令:
# build - 仅构建镜像
# start - 构建并启动服务
# stop - 停止服务
# restart - 重启服务
# logs - 查看日志
# status - 查看服务状态
# clean - 清理容器和镜像
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 项目信息
PROJECT_NAME="genex-admin-web"
IMAGE_NAME="genex-admin-web"
CONTAINER_NAME="genex-admin-web"
DEFAULT_PORT=3000
# 日志函数
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 检查 Docker 是否安装
check_docker() {
if ! command -v docker &> /dev/null; then
log_error "Docker 未安装,请先安装 Docker"
exit 1
fi
if ! docker info &> /dev/null; then
log_error "Docker 服务未运行,请启动 Docker"
exit 1
fi
log_success "Docker 检查通过"
}
# 检查 Docker Compose 是否安装
check_docker_compose() {
if docker compose version &> /dev/null; then
COMPOSE_CMD="docker compose"
elif command -v docker-compose &> /dev/null; then
COMPOSE_CMD="docker-compose"
else
log_error "Docker Compose 未安装"
exit 1
fi
log_success "Docker Compose 检查通过 ($COMPOSE_CMD)"
}
# 构建镜像
build() {
log_info "开始构建 Docker 镜像..."
$COMPOSE_CMD build --no-cache
log_success "镜像构建完成"
}
# 启动服务
start() {
log_info "开始部署 Genex Admin Web..."
# 检查端口是否被占用
PORT=${PORT:-$DEFAULT_PORT}
if lsof -Pi :$PORT -sTCP:LISTEN -t >/dev/null 2>&1; then
log_warn "端口 $PORT 已被占用,尝试停止旧服务..."
stop
fi
# 构建并启动
$COMPOSE_CMD up -d --build
# 等待服务启动
log_info "等待服务启动..."
sleep 5
# 检查服务状态
if docker ps | grep -q $CONTAINER_NAME; then
log_success "服务部署成功!"
log_info "访问地址: http://localhost:$PORT"
log_info "健康检查: http://localhost:$PORT/api/health"
else
log_error "服务启动失败,请查看日志: ./deploy.sh logs"
exit 1
fi
}
# 停止服务
stop() {
log_info "停止服务..."
$COMPOSE_CMD down
log_success "服务已停止"
}
# 重启服务
restart() {
log_info "重启服务..."
stop
start
}
# 查看日志
logs() {
$COMPOSE_CMD logs -f
}
# 清理
clean() {
log_info "清理容器和镜像..."
# 停止并删除容器
$COMPOSE_CMD down --rmi local --volumes --remove-orphans
# 删除悬空镜像
docker image prune -f
log_success "清理完成"
}
# 显示状态
status() {
log_info "服务状态:"
docker ps -a --filter "name=$CONTAINER_NAME" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
}
# 显示帮助
show_help() {
echo ""
echo "Genex Admin Web 部署脚本"
echo ""
echo "使用方法: ./deploy.sh [命令]"
echo ""
echo "命令:"
echo " build 仅构建 Docker 镜像"
echo " start 构建并启动服务 (默认)"
echo " stop 停止服务"
echo " restart 重启服务"
echo " logs 查看服务日志"
echo " status 查看服务状态"
echo " clean 清理容器和镜像"
echo " help 显示此帮助信息"
echo ""
echo "环境变量:"
echo " PORT 服务端口 (默认: 3000)"
echo ""
echo "示例:"
echo " ./deploy.sh start # 默认端口 3000 启动"
echo " PORT=8080 ./deploy.sh start # 指定端口 8080 启动"
echo ""
}
# 主函数
main() {
# 切换到脚本所在目录
cd "$(dirname "$0")"
# 检查环境
check_docker
check_docker_compose
# 执行命令
case "${1:-start}" in
build)
build
;;
start)
start
;;
stop)
stop
;;
restart)
restart
;;
logs)
logs
;;
status)
status
;;
clean)
clean
;;
help|--help|-h)
show_help
;;
*)
log_error "未知命令: $1"
show_help
exit 1
;;
esac
}
main "$@"

View File

@ -0,0 +1,26 @@
services:
admin-web:
build:
context: .
dockerfile: Dockerfile
image: genex-admin-web:latest
container_name: genex-admin-web
restart: unless-stopped
ports:
- "${PORT:-3000}:3000"
environment:
- TZ=Asia/Shanghai
- NODE_ENV=production
- NEXT_TELEMETRY_DISABLED=1
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/api/health"]
interval: 30s
timeout: 3s
retries: 3
start_period: 40s
networks:
- genex-network
networks:
genex-network:
driver: bridge