# ========================== # Stage 1: Build the Go binary # ========================== FROM golang:1.21-alpine AS builder # 设置工作目录 WORKDIR /build # 复制 go.mod 和 go.sum,先做依赖缓存 COPY go.mod go.sum ./ # 下载依赖(缓存层) RUN go mod download # 复制源代码 COPY app/ ./app # 编译可执行文件 RUN go build -o sentinel ./app/main.go # ========================== # Stage 2: 生成最终运行镜像 # ========================== FROM alpine:3.18 # 安装 ca-certificates,如果程序需要访问 HTTPS RUN apk add --no-cache ca-certificates # 设置工作目录 WORKDIR /app # 复制从 builder 构建好的二进制 COPY --from=builder /build/sentinel . # 设置默认执行命令 CMD ["./sentinel"]