Skip to content

Commit c375627

Browse files
authored
Merge pull request #558 from pjbgf/musl-tool-chain
Statically build using musl toolchain and target alpine
2 parents 1c2ee29 + d163eba commit c375627

File tree

2 files changed

+35
-64
lines changed

2 files changed

+35
-64
lines changed

Dockerfile

+34-63
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,15 @@
1-
ARG BASE_VARIANT=bullseye
1+
ARG BASE_VARIANT=alpine
22
ARG GO_VERSION=1.17
33
ARG XX_VERSION=1.1.0
44

55
ARG 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
5515
WORKDIR /workspace
@@ -64,35 +24,46 @@ COPY go.sum go.sum
6424
# Cache modules
6525
RUN 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
7430
ENV CGO_ENABLED=1
7531
ARG 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

9057
ARG 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/
9667
COPY ATTRIBUTIONS.md /
9768

9869
USER 65534:65534

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ TAG ?= latest
44

55
# Base image used to build the Go binary
66
LIBGIT2_IMG ?= ghcr.io/fluxcd/golang-with-libgit2
7-
LIBGIT2_TAG ?= libgit2-1.1.1-3
7+
LIBGIT2_TAG ?= libgit2-1.1.1-4
88

99
# Allows for defining additional Docker buildx arguments,
1010
# e.g. '--push'.

0 commit comments

Comments
 (0)