Skip to content

Commit 0543dda

Browse files
committed
Build controller without CGO
Signed-off-by: Stefan Prodan <[email protected]>
1 parent 912feb4 commit 0543dda

File tree

1 file changed

+7
-32
lines changed

1 file changed

+7
-32
lines changed

Dockerfile

+7-32
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
1-
ARG BASE_VARIANT=alpine
21
ARG GO_VERSION=1.23
32
ARG XX_VERSION=1.6.1
43

54
FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
65

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
168

9+
# Copy the build utilities.
1710
COPY --from=xx / /
1811

19-
# build-go-mod can still be cached at build platform architecture.
20-
FROM build-base AS build
21-
2212
ARG TARGETPLATFORM
2313

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-
2814
# Configure workspace
2915
WORKDIR /workspace
3016

@@ -46,28 +32,17 @@ COPY internal/ internal/
4632
ARG TARGETPLATFORM
4733
ARG TARGETARCH
4834

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
6238

6339
FROM alpine:3.21
6440

6541
ARG TARGETPLATFORM
6642
RUN apk --no-cache add ca-certificates \
6743
&& update-ca-certificates
6844

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/
7146

7247
USER 65534:65534
7348
ENTRYPOINT [ "source-controller" ]

0 commit comments

Comments
 (0)