1- ARG BASE_VARIANT=bullseye
1+ ARG BASE_VARIANT=alpine
22ARG GO_VERSION=1.17
33ARG XX_VERSION=1.1.0
44
55ARG LIBGIT2_IMG=ghcr.io/fluxcd/golang-with-libgit2
6- ARG LIBGIT2_TAG=libgit2-1.1.1-3
6+ ARG LIBGIT2_TAG=libgit2-1.1.1-4
77
8- FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
9- FROM ${LIBGIT2_IMG}:${LIBGIT2_TAG} as libgit2
8+ FROM --platform=linux/amd64 ${LIBGIT2_IMG}:${LIBGIT2_TAG} as build-amd64
9+ FROM --platform=linux/arm64 ${LIBGIT2_IMG}:${LIBGIT2_TAG} as build-arm64
10+ FROM --platform=linux/arm/v7 ${LIBGIT2_IMG}:${LIBGIT2_TAG} as build-armv7
1011
11- FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-${BASE_VARIANT} as gostable
12-
13- FROM gostable AS go-linux
14-
15- FROM go-${TARGETOS} AS build-base-bullseye
16-
17- # Copy the build utilities
18- COPY --from=xx / /
19-
20- # Align golang base image with bookworm.
21- # TODO: Replace this with a golang bookworm variant, once that is released.
22- RUN echo "deb http://deb.debian.org/debian bookworm main" > /etc/apt/sources.list.d/bookworm.list \
23- && echo "deb-src http://deb.debian.org/debian bookworm main" /etc/apt/sources.list.d/bookworm.list \
24- && xx-apt update \
25- && xx-apt -t bookworm upgrade -y \
26- && xx-apt -t bookworm install -y curl
27-
28- COPY --from=libgit2 /Makefile /libgit2/
29-
30- # Install the libgit2 build dependencies
31- RUN make -C /libgit2 cmake
32-
33- ARG TARGETPLATFORM
34- RUN make -C /libgit2 dependencies
35-
36- FROM build-base-${BASE_VARIANT} as libgit2-bullseye
37-
38- ARG TARGETPLATFORM
39-
40- # First build libgit2 statically, this ensures that all its dependencies
41- # will be statically available as well.
42- ARG BUILD_SHARED_LIBS=OFF
43- RUN FLAGS=$(xx-clang --print-cmake-defines) make -C /libgit2 libgit2
44-
45- # Rebuild libgit2 this time to generate the shared libraries.
46- ARG BUILD_SHARED_LIBS=ON
47- RUN FLAGS=$(xx-clang --print-cmake-defines) make -C /libgit2 libgit2
48- # Logs glibc version used at built time. The final image must be compatible with it.
49- RUN ldd --version ldd > /libgit2/built-on-glibc-version
50-
51-
52- FROM libgit2-${BASE_VARIANT} as build
12+ FROM --platform=$BUILDPLATFORM build-$TARGETARCH$TARGETVARIANT AS build
5313
5414# Configure workspace
5515WORKDIR /workspace
@@ -64,35 +24,46 @@ COPY go.sum go.sum
6424# Cache modules
6525RUN go mod download
6626
67- # Copy source code
68- COPY main.go main.go
69- COPY controllers/ controllers/
70- COPY pkg/ pkg/
71- COPY internal/ internal/
27+ RUN apk add clang lld pkgconfig ca-certificates
7228
7329# Build the binary
7430ENV CGO_ENABLED=1
7531ARG TARGETPLATFORM
7632
77- # The dependencies being statically built are: libgit2, libssh2, libssl, libcrypto and libz.
78- # Others (such as libc, librt, libdl and libpthread) are resolved at run-time.
79- # To decrease the likelihood of such dependencies being out of sync, the base build image
80- # should be aligned with the target (i.e. same debian variant).
81- RUN FLAGS=$(pkg-config --static --libs --cflags libssh2 libgit2 libssl libcrypto zlib openssl) \
33+ RUN xx-apk add --no-cache \
34+ musl-dev gcc lld binutils-gold
35+
36+ # Performance related changes:
37+ # - Use read-only bind instead of copying go source files.
38+ # - Cache go packages.
39+ RUN --mount=target=. \
40+ --mount=type=cache,target=/root/.cache/go-build \
41+ --mount=type=cache,target=/go/pkg \
42+ export LIBRARY_PATH="/usr/local/$(xx-info triple)/lib:/usr/local/$(xx-info triple)/lib64:${LIBRARY_PATH}" && \
43+ export PKG_CONFIG_PATH="/usr/local/$(xx-info triple)/lib/pkgconfig:/usr/local/$(xx-info triple)/lib64/pkgconfig" && \
44+ export FLAGS="$(pkg-config --static --libs --cflags libssh2 openssl libgit2)" && \
45+ CGO_LDFLAGS="${FLAGS} -static" \
8246 xx-go build \
83- -ldflags "-s -w -extldflags \" /usr/lib/$(xx-info triple)/libssh2.a /usr/lib/$(xx-info triple)/libssl.a /usr/lib/$(xx-info triple)/libcrypto.a /usr/lib/$(xx-info triple)/libz.a -Wl,--unresolved-symbols=ignore-in-object-files -Wl,-allow-shlib-undefined ${FLAGS} -static \" " \
47+ -ldflags "-s -w" \
8448 -tags 'netgo,osusergo,static_build' \
85- -o source-controller -trimpath main.go;
49+ -o /source-controller -trimpath main.go;
50+
51+ # Ensure that the binary was cross-compiled correctly to the target platform.
52+ RUN xx-verify --static /source-controller
8653
87- # The target image must aligned with apt sources used for libgit2.
88- FROM debian:bookworm-slim as controller
54+
55+ FROM alpine
8956
9057ARG TARGETPLATFORM
91- RUN apt update && apt install -y ca-certificates
58+ RUN apk --no-cache add ca-certificates \
59+ && update-ca-certificates
60+
61+ # Create minimal nsswitch.conf file to prioritize the usage of /etc/hosts over DNS queries.
62+ # https://github.com/gliderlabs/docker-alpine/issues/367#issuecomment-354316460
63+ RUN [ ! -e /etc/nsswitch.conf ] && echo 'hosts: files dns' > /etc/nsswitch.conf
9264
9365# Copy over binary from build
94- COPY --from=build /workspace/source-controller /usr/local/bin/
95- COPY --from=libgit2-bullseye /libgit2/built-on-glibc-version /
66+ COPY --from=build /source-controller /usr/local/bin/
9667COPY ATTRIBUTIONS.md /
9768
9869USER 65534:65534
0 commit comments