- 新增 scripts/setup-alpine-mirrors.sh 探测可用镜像 - 移除硬编码清华源,runtime 固定 alpine:3.20 避免 latest 漂移
128 lines
4.9 KiB
Docker
128 lines
4.9 KiB
Docker
# syntax=docker/dockerfile:1
|
|
# ============================================================
|
|
# AIOps Backend + Agent 多阶段 Dockerfile (BuildKit 加速版)
|
|
# Stage 1: 编译后端 (Go 1.24)
|
|
# Stage 2: 交叉编译 Agent (Go 1.24, linux-amd64/arm64 + darwin-arm64)
|
|
# Stage 3: 运行时 (Alpine 3.20 固定版本)
|
|
# 署名 Logan.Li
|
|
# ============================================================
|
|
|
|
# 构建参数 — 版本号通过 git tag 或环境变量注入
|
|
ARG VERSION=dev
|
|
ARG BUILD_TIME=unknown
|
|
ARG KUBECTL_VERSION=v1.32.2
|
|
|
|
# ==================== Stage 1: Backend Builder ====================
|
|
FROM golang:1.24-alpine AS builder
|
|
|
|
WORKDIR /app
|
|
|
|
# Alpine 镜像源探测(清华 403 时自动回退)// 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(利用 Docker 层缓存)
|
|
COPY backend/go.mod backend/go.sum ./
|
|
# --mount=type=cache 让 go mod download 的结果跨构建复用 // Logan.Li
|
|
RUN --mount=type=cache,target=/go/pkg/mod \
|
|
--mount=type=cache,target=/root/.cache/go-build \
|
|
go mod download
|
|
|
|
# 复制后端源码
|
|
COPY backend/ ./
|
|
|
|
# 构建后端二进制(注入版本号)— cache mount 让增量编译极快
|
|
RUN --mount=type=cache,target=/go/pkg/mod \
|
|
--mount=type=cache,target=/root/.cache/go-build \
|
|
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
|
|
-ldflags="-w -s -X main.Version=${VERSION} -X main.BuildTime=${BUILD_TIME}" \
|
|
-o aiops .
|
|
|
|
# ==================== Stage 2: Agent Builder ====================
|
|
FROM golang:1.24-alpine AS agent-builder
|
|
|
|
ARG VERSION
|
|
ARG BUILD_TIME
|
|
|
|
WORKDIR /agent
|
|
|
|
ENV GOPROXY=https://goproxy.cn,direct
|
|
ENV GOSUMDB=off
|
|
ENV GOMODCACHE=/go/pkg/mod
|
|
ENV GOCACHE=/root/.cache/go-build
|
|
|
|
# 复制 agent go.mod
|
|
COPY agent/go.mod agent/go.sum ./
|
|
RUN --mount=type=cache,target=/go/pkg/mod \
|
|
--mount=type=cache,target=/root/.cache/go-build \
|
|
go mod download
|
|
|
|
# 复制 agent 源码
|
|
COPY agent/ ./
|
|
|
|
# 交叉编译三个平台的 agent 二进制(注入版本号)
|
|
RUN --mount=type=cache,target=/go/pkg/mod \
|
|
--mount=type=cache,target=/root/.cache/go-build \
|
|
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w -X main.version=${VERSION} -X main.buildTime=${BUILD_TIME}" -o bin/aiops-agent-linux-amd64 . && \
|
|
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-s -w -X main.version=${VERSION} -X main.buildTime=${BUILD_TIME}" -o bin/aiops-agent-linux-arm64 . && \
|
|
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w -X main.version=${VERSION} -X main.buildTime=${BUILD_TIME}" -o bin/aiops-agent-darwin-arm64 .
|
|
|
|
# ==================== Stage 3: Runtime ====================
|
|
# 固定版本避免 :latest 每次变动导致全量 cache 失效 // Logan.Li
|
|
FROM alpine:3.20
|
|
|
|
ARG TARGETARCH
|
|
ARG KUBECTL_VERSION=v1.32.2
|
|
|
|
# 安装运行时依赖(核心最小集,重型工具改为懒加载)// 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 git docker-cli docker-cli-buildx && \
|
|
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
|
|
|
|
# npm 淘宝镜像加速(node 懒加载后生效)
|
|
RUN npm config set registry https://registry.npmmirror.com 2>/dev/null || true
|
|
# Maven 阿里云镜像加速(maven 懒加载后生效)
|
|
RUN mkdir -p /root/.m2 && \
|
|
echo '<?xml version="1.0" encoding="UTF-8"?><settings><mirrors><mirror><id>aliyun</id><mirrorOf>central</mirrorOf><name>Aliyun Maven</name><url>https://maven.aliyun.com/repository/public</url></mirror></mirrors></settings>' > /root/.m2/settings.xml
|
|
|
|
ENV TZ=Asia/Shanghai
|
|
|
|
WORKDIR /app
|
|
|
|
# 复制后端二进制
|
|
COPY --from=builder /app/aiops .
|
|
|
|
# 创建运行时目录(agent 二进制 + 安装脚本)
|
|
RUN mkdir -p /app/agent/bin /app/agent/deploy
|
|
|
|
# 复制 agent 二进制(三个平台)
|
|
COPY --from=agent-builder /agent/bin/aiops-agent-linux-amd64 /app/agent/bin/
|
|
COPY --from=agent-builder /agent/bin/aiops-agent-linux-arm64 /app/agent/bin/
|
|
COPY --from=agent-builder /agent/bin/aiops-agent-darwin-arm64 /app/agent/bin/
|
|
|
|
# 复制 agent 安装脚本和 systemd service 模板
|
|
COPY agent/deploy/install.sh /app/agent/deploy/
|
|
COPY agent/deploy/aiops-agent.service /app/agent/deploy/
|
|
|
|
# 启动脚本(注入 hosts DNS 缓存)
|
|
COPY backend/entrypoint.sh /app/
|
|
RUN chmod +x /app/entrypoint.sh
|
|
|
|
EXPOSE 8080
|
|
|
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
|
|
CMD curl -f http://localhost:8080/health || exit 1
|
|
|
|
CMD ["./entrypoint.sh"]
|