1
- ARG BASE_VARIANT=alpine
2
1
ARG GO_VERSION=1.23
3
2
ARG XX_VERSION=1.6.1
4
3
5
4
FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
6
5
7
- FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-${BASE_VARIANT} AS gostable
8
-
9
- FROM gostable AS go-linux
10
-
11
- # Build-base consists of build platform dependencies and xx.
12
- # These will be used at current arch to yield execute the cross compilations.
13
- FROM go-${TARGETOS} AS build-base
14
-
15
- RUN apk add --no-cache clang lld
6
+ # Docker buildkit multi-arch build requires golang alpine
7
+ FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS builder
16
8
9
+ # Copy the build utilities.
17
10
COPY --from=xx / /
18
11
19
- # build-go-mod can still be cached at build platform architecture.
20
- FROM build-base AS build
21
-
22
12
ARG TARGETPLATFORM
23
13
24
- # Some dependencies have to installed
25
- # for the target platform: https://github.com/tonistiigi/xx#go--cgo
26
- RUN xx-apk add musl-dev gcc clang lld
27
-
28
14
# Configure workspace
29
15
WORKDIR /workspace
30
16
@@ -46,28 +32,17 @@ COPY internal/ internal/
46
32
ARG TARGETPLATFORM
47
33
ARG TARGETARCH
48
34
49
- # Reasons why CGO is in use:
50
- # - The SHA1 implementation (sha1cd) used by go-git depends on CGO for
51
- # performance reasons. See: https://github.com/pjbgf/sha1cd/issues/15
52
- ENV CGO_ENABLED=1
53
-
54
- RUN export CGO_LDFLAGS="-static -fuse-ld=lld" && \
55
- xx-go build \
56
- -ldflags "-s -w" \
57
- -tags 'netgo,osusergo,static_build' \
58
- -o /source-controller -trimpath main.go;
59
-
60
- # Ensure that the binary was cross-compiled correctly to the target platform.
61
- RUN xx-verify --static /source-controller
35
+ # build without specifing the arch
36
+ ENV CGO_ENABLED=0
37
+ RUN xx-go build -trimpath -a -o source-controller main.go
62
38
63
39
FROM alpine:3.21
64
40
65
41
ARG TARGETPLATFORM
66
42
RUN apk --no-cache add ca-certificates \
67
43
&& update-ca-certificates
68
44
69
- # Copy over binary from build
70
- COPY --from=build /source-controller /usr/local/bin/
45
+ COPY --from=builder /workspace/source-controller /usr/local/bin/
71
46
72
47
USER 65534:65534
73
48
ENTRYPOINT [ "source-controller" ]
0 commit comments