Alpine 编译 CGO 时,需要添加一些依赖包。

包名说明
tzdata时区
ca-certificatesCA 证书库
libc6-compat兼容 glibc
libgccgcc 运行库 CGO 依赖
libstdc++C++ 运行库

以下为示例 Dockerfile 内容:

 1#build stage
 2FROM golang:1.23-alpine AS builder
 3ARG BUILD_VERSION
 4ARG BUILD_TIME
 5WORKDIR /app
 6COPY . .
 7RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
 8RUN apk add -U tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
 9RUN go build -ldflags "-s -w -X ceic/global.AppVersion=${BUILD_VERSION}  -X ceic/global.AppBuild=${BUILD_TIME}" -mod=vendor -v -o ceic .
10
11#final stage
12FROM alpine:latest
13ARG BUILD_VERSION
14WORKDIR /app
15RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
16RUN apk --no-cache add libc6-compat libgcc libstdc++
17COPY --from=builder /etc/localtime /etc/localtime
18COPY --from=builder /app/ceic /app/ceic
19ENTRYPOINT ["/app/ceic", "serve"]
20LABEL Name=ceic Version=${BUILD_VERSION}
21EXPOSE 3000