Files
aiops/Dockerfile
T
Logan.Li fd60361d93 chore: 统一本地服务器与远程仓库源码
- 合并 main 与 master 历史并以服务器结构为基线
- 保留迁移、数据保留、AWS 账单与部署安全能力
- 建立 SDD 规格和 CI 验证闭环
2026-08-05 20:03:25 +08:00

150 lines
5.1 KiB
Docker
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.
# ============================================================
# AIOps Embed Dockerfile — 前端嵌入后端二进制,单容器部署
# Stage 1: 构建前端 (Node 20)
# Stage 2: 构建后端 (Go 1.24, go:embed 前端)
# Stage 3: Agent 交叉编译
# Stage 4: 运行时 (Alpine, 单二进制服务前后端)
# 署名 Logan.Li
# ============================================================
ARG VERSION=dev
ARG BUILD_TIME=unknown
ARG GIT_COMMIT=unknown
ARG KUBECTL_VERSION=v1.32.2
# ==================== Stage 1: Frontend Builder ====================
FROM node:20-alpine AS frontend-builder
WORKDIR /app
# 限制 Node 内存,避免 2 核小机器 docker build 时 OOM/高负载 // Logan.Li
ENV NODE_OPTIONS="--max-old-space-size=2048"
# 使用淘宝镜像加速
RUN npm config set registry https://registry.npmmirror.com
# 安装依赖(利用 Docker 缓存层)
COPY frontend/package.json frontend/package-lock.json ./
RUN npm ci
# 复制源代码并构建
COPY frontend/ ./
RUN npm run build
# ==================== Stage 2: Backend Builder ====================
FROM golang:1.25-alpine AS backend-builder
ARG VERSION
ARG BUILD_TIME
ARG GIT_COMMIT
ARG TARGETARCH
WORKDIR /app
# 安装构建依赖(纯 Go SQLite 无需 gcc/musl-dev)// Logan.Li
COPY scripts/setup-alpine-mirrors.sh /tmp/setup-alpine-mirrors.sh
RUN chmod +x /tmp/setup-alpine-mirrors.sh && /tmp/setup-alpine-mirrors.sh && \
apk add --no-cache git ca-certificates tzdata
ENV TZ=Asia/Shanghai
ENV GOPROXY=https://goproxy.cn,direct
ENV GONOSUMCHECK=*
ENV GOMODCACHE=/go/pkg/mod
ENV GOCACHE=/root/.cache/go-build
# 复制后端 go.mod,第三方 RDP 兜底依赖已移除,后端使用自研 RDP 引擎。 // Logan.Li
COPY backend/go.mod backend/go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download
# 复制后端源码
COPY backend/ ./
# ★ 核心:将前端构建产物复制到 embed 目录
RUN rm -rf static && mkdir -p static
COPY --from=frontend-builder /app/dist/ static/
# 构建后端二进制(纯 Go 无 CGO,cache mount 增量编译)// Logan.Li
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg/mod \
CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} go build \
-ldflags="-w -s -X main.Version=${VERSION} -X main.BuildTime=${BUILD_TIME} -X main.GitCommit=${GIT_COMMIT}" \
-o aiops .
# ==================== Stage 3: Agent Builder ====================
FROM golang:1.24-alpine AS agent-builder
ARG VERSION
ARG BUILD_TIME
ARG TARGETARCH
WORKDIR /agent
ENV GOPROXY=https://goproxy.cn,direct
ENV GOSUMDB=off
COPY agent/go.mod agent/go.sum ./
RUN go mod download
COPY agent/ ./
# 编译容器目标架构 Linux Agent,同时固定产出 Windows AMD64 便于 Windows 主机下载安装。 // Logan.Li
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} go build \
-ldflags="-s -w -X main.version=${VERSION} -X main.buildTime=${BUILD_TIME}" \
-o bin/aiops-agent-linux-${TARGETARCH} .
RUN CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build \
-ldflags="-s -w -X main.version=${VERSION} -X main.buildTime=${BUILD_TIME}" \
-o bin/aiops-agent-windows-amd64.exe .
# ==================== Stage 4: Runtime ====================
FROM alpine:3.20
ARG TARGETARCH
ARG KUBECTL_VERSION=v1.32.2
# 安装运行时依赖(U4 镜像瘦身 — 核心最小集, git/docker-cli 改为懒加载)// Logan.Li
# kubectl 通过 install-kubectl.sh 下载(避免构建上下文携带 60MB 二进制)// Logan.Li
COPY scripts/setup-alpine-mirrors.sh scripts/install-kubectl.sh /tmp/
RUN chmod +x /tmp/setup-alpine-mirrors.sh && /tmp/setup-alpine-mirrors.sh && \
apk add --no-cache ca-certificates tzdata bash curl python3 && \
chmod +x /tmp/install-kubectl.sh && \
TARGET_OS=linux TARGET_ARCH="${TARGETARCH}" KUBECTL_VERSION="${KUBECTL_VERSION}" \
/tmp/install-kubectl.sh /usr/local/bin/kubectl && \
rm -f /tmp/setup-alpine-mirrors.sh /tmp/install-kubectl.sh
ENV TZ=Asia/Shanghai
WORKDIR /app
# 复制后端二进制(前端已嵌入)
COPY --from=backend-builder /app/aiops .
# 创建非 root 运行用户及应用可写目录。工具懒加载目录由该用户写入。 // Logan.Li
RUN addgroup -S -g 10001 aiops && \
adduser -S -D -H -u 10001 -G aiops aiops && \
mkdir -p /app/agent/bin /app/agent/deploy /app/uploads /app/logs /app/tmp \
/home/aiops && \
chown -R aiops:aiops /app /home/aiops /usr/local && \
chmod 1777 /tmp
# 复制 agent 二进制。 // Logan.Li
COPY --from=agent-builder /agent/bin/aiops-agent-linux-${TARGETARCH} /app/agent/bin/aiops-agent-linux-${TARGETARCH}
COPY --from=agent-builder /agent/bin/aiops-agent-windows-amd64.exe /app/agent/bin/aiops-agent-windows-amd64.exe
# 复制 agent 安装脚本
COPY agent/deploy/install.sh /app/agent/deploy/
COPY agent/deploy/install.ps1 /app/agent/deploy/
COPY agent/deploy/aiops-agent.service /app/agent/deploy/
# 应用进程不得使用 root;部署清单还会限制 Linux capabilities。 // Logan.Li
USER 10001:10001
ENV HOME=/home/aiops \
TMPDIR=/app/tmp
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
CMD curl -f http://localhost:8080/health || exit 1
CMD ["./aiops"]