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- COPY --from=libgit2 /Makefile /libgit2/
20-
21- # Install the libgit2 build dependencies
22- RUN make -C /libgit2 cmake
23-
24- ARG TARGETPLATFORM
25- RUN make -C /libgit2 dependencies
26-
27- FROM build-base-${BASE_VARIANT} as libgit2-bullseye
28-
29- # Compile and install libgit2
30- ARG TARGETPLATFORM
31- RUN FLAGS=$(xx-clang --print-cmake-defines) make -C /libgit2 libgit2
32-
33- FROM libgit2-${BASE_VARIANT} as build
12+ FROM --platform=$BUILDPLATFORM build-$TARGETARCH$TARGETVARIANT AS build
3413
3514# Configure workspace
3615WORKDIR /workspace
@@ -47,43 +26,47 @@ COPY go.sum go.sum
4726# and so that source changes don't invalidate our downloaded layer
4827RUN go mod download
4928
50- # Copy the go source
51- COPY main.go main.go
52- COPY pkg/ pkg/
53- COPY controllers/ controllers/
29+ RUN apk add clang lld pkgconfig ca-certificates
5430
55- # Build the binary
5631ENV CGO_ENABLED=1
5732ARG TARGETPLATFORM
58- RUN xx-go build -o image-automation-controller -trimpath \
33+
34+ RUN xx-apk add --no-cache \
35+ musl-dev gcc lld binutils-gold
36+
37+ # Performance related changes:
38+ # - Use read-only bind instead of copying go source files.
39+ # - Cache go packages.
40+ RUN --mount=target=. \
41+ --mount=type=cache,target=/root/.cache/go-build \
42+ --mount=type=cache,target=/go/pkg \
43+ export LIBRARY_PATH="/usr/local/$(xx-info triple)/lib:/usr/local/$(xx-info triple)/lib64:${LIBRARY_PATH}" && \
44+ export PKG_CONFIG_PATH="/usr/local/$(xx-info triple)/lib/pkgconfig:/usr/local/$(xx-info triple)/lib64/pkgconfig" && \
45+ export FLAGS="$(pkg-config --static --libs --cflags libssh2 openssl libgit2)" && \
46+ CGO_LDFLAGS="${FLAGS} -static" \
47+ xx-go build \
48+ -ldflags "-s -w" \
49+ -tags 'netgo,osusergo,static_build' \
50+ -o /image-automation-controller -trimpath \
5951 main.go
6052
61- FROM build as prepare-bullseye
53+ # Ensure that the binary was cross-compiled correctly to the target platform.
54+ RUN xx-verify --static /image-automation-controller
6255
63- # Move libgit2 lib to generic and predictable location
64- ARG TARGETPLATFORM
65- RUN mkdir -p /libgit2/lib/ \
66- && cp -d /usr/lib/$(xx-info triple)/libgit2.so* /libgit2/lib/
6756
68- FROM prepare-${BASE_VARIANT} as prepare
57+ FROM alpine:3.15
6958
70- # The target image must aligned with apt sources used for libgit2.
71- FROM debian:bookworm-slim as controller
72-
73- # Copy libgit2
74- COPY --from=prepare /libgit2/lib/ /usr/local/lib/
75- RUN ldconfig
59+ ARG TARGETPLATFORM
60+ RUN apk --no-cache add ca-certificates \
61+ && update-ca-certificates
7662
77- # Upgrade packages and install runtime dependencies
78- RUN apt update \
79- && apt install -y zlib1g libssl1.1 libssh2-1 ca-certificates \
80- && apt clean \
81- && apt autoremove --purge -y \
82- && rm -rf /var/lib/apt/lists/*
63+ # Create minimal nsswitch.conf file to prioritize the usage of /etc/hosts over DNS queries.
64+ # https://github.com/gliderlabs/docker-alpine/issues/367#issuecomment-354316460
65+ RUN [ ! -e /etc/nsswitch.conf ] && echo 'hosts: files dns' > /etc/nsswitch.conf
8366
8467# Copy over binary from build
85- COPY --from=prepare /workspace/image-automation-controller /usr/local/bin/
68+ COPY --from=build /image-automation-controller /usr/local/bin/
69+ COPY ATTRIBUTIONS.md /
8670
8771USER 65534:65534
88-
8972ENTRYPOINT [ "image-automation-controller" ]
0 commit comments