Build a golang image with cgo support on alpine, we need to install some packages.
Package | Description |
---|---|
tzdata | Timezone |
ca-certificates | CA library |
libc6-compat | glibc compatibility |
libgcc | CGO dependency |
libstdc++ | C++ runtime library |
Below is an example 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