perf: Docker build 提速 — 去 gcc/musl-dev + CGO_ENABLED=0 + cache mount

- 移除 gcc musl-dev (纯 Go SQLite 不需要 CGO)
- CGO_ENABLED=1 → 0 (编译提速 ~2-3x)
- go mod download/go build 加 --mount=type=cache 跨构建复用
This commit is contained in:
attacker committed 2026-06-27 02:03:34 +00:00
1 parent 92a2247390
commit 5f83d6e650
1 file changed
+10 -5
+10 -5
View File
@@ -38,18 +38,21 @@ ARG TARGETARCH
WORKDIR /app
# Alpine 镜像源探测(清华 403 时自动回退阿里云/官方)// Logan.Li
# 安装构建依赖(纯 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 gcc musl-dev
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 ./
RUN go mod download
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download
# 复制后端源码
COPY backend/ ./
@@ -58,8 +61,10 @@ COPY backend/ ./
RUN rm -rf static && mkdir -p static
COPY --from=frontend-builder /app/dist/ static/
# 构建后端二进制(跟随 TARGETARCH,Mac ARM 构建 linux/arm64 避免 CGO 交叉编译失败)// Logan.Li
RUN CGO_ENABLED=1 GOOS=linux GOARCH=${TARGETARCH} go build \
# 构建后端二进制(纯 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 .