sdk_generate/Dockerfile

51 lines
1.4 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.

# ============================================================
# 阶段1: 构建(精简版)
# ============================================================
FROM golang:1.26-alpine AS builder
WORKDIR /app
# 合并安装 + 环境变量
RUN apk add --no-cache git make && \
go env -w GOPROXY=https://goproxy.cn,direct && \
go env -w GOSUMDB=sum.golang.google.cn
# 利用 Docker 缓存,依赖不变则复用
COPY go.mod go.sum ./
RUN go mod download
# 复制源码并构建(合并 COPY 和 RUN
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-ldflags="-s -w" \
-o /app/sdk-generator .
# ============================================================
# 阶段2: 运行(极简版)
# ============================================================
FROM alpine:latest
# 安装必要包 + 创建用户(合并)
RUN apk add --no-cache ca-certificates tzdata && \
adduser -D -g '' appuser
# 时区(精简)
ENV TZ=Asia/Shanghai
WORKDIR /app
# 复制二进制和静态文件
COPY --from=builder /app/sdk-generator .
COPY --chown=appuser:appuser ./web ./web
# 创建目录并授权(合并)
RUN mkdir -p /app/outputs /app/uploads && chown -R appuser:appuser /app
USER appuser
EXPOSE 5002
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1
ENTRYPOINT ["/app/sdk-generator"]