-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
43 lines (31 loc) · 1.08 KB
/
Copy pathDockerfile
File metadata and controls
43 lines (31 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
ARG GO_BASE_IMAGE=golang:1.24-alpine
# Docker buildx 会自动提供这些构建参数
# 如果使用传统 docker build,这些参数可能未设置
ARG TARGETPLATFORM
ARG BUILDPLATFORM
ARG TARGETOS=linux
ARG TARGETARCH
# 如果 BUILDPLATFORM 未设置,则不指定 --platform(使用默认)
FROM ${GO_BASE_IMAGE} AS builder
ENV GOPROXY=https://goproxy.cn,direct
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
# 根据目标架构交叉编译
# TARGETOS 和 TARGETARCH 由 Docker buildx 自动提供
# 如果 TARGETARCH 未设置(使用传统 docker build),则使用当前架构
RUN set -e && \
if [ -z "$TARGETARCH" ]; then \
export TARGETARCH=$(go env GOARCH); \
fi && \
echo "Building for GOOS=${TARGETOS} GOARCH=${TARGETARCH}" && \
CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
go build -trimpath -ldflags "-s -w" -o /out/go-pac .
FROM scratch AS runner
WORKDIR /opt/go-pac
COPY --from=builder /out/go-pac /opt/go-pac/go-pac
COPY config.yaml /opt/go-pac/
EXPOSE 80
ENTRYPOINT ["/opt/go-pac/go-pac"]
CMD ["-port", "80"]