-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: improve container image build times
Essentially this adds --platform=${BUILDPLATFORM} to the FROM instruction for the build stage. This makes that stage run on the platform of the underlying machine instead running in the target platform/architecture. We then let the Go compiler take care of producing a binary for the target platform by setting GOARCH to TARGETPLATFORM. This massively speeds up multi-platform builds, as it removes the need for emulation in the compilation phase. Before, building all images for both platforms (x86_64 and arm64) on cold caches via GitHub actions would take around 1:13 h, with individual image build times roughly as follows: - operator controller: 15 minutes - instrumentation: 4 minutes - collector: 37 minutes - configuration reloader: 4 minutes - filelog offset synch: 11 minutes With this change, building all images for both platforms (x86_64 and arm64) on cold caches via GitHub actions takes only 16 minutes, with individual image build times roughly as follows: - operator controller: 3 minutes - instrumentation: 4 minutes - collector: 6 minutes - configuration reloader: 1 minute - filelog offset synch: 2 minutes Note: The instrumentation image is not yet migrated to this new strategy.
- Loading branch information
Showing
4 changed files
with
60 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,38 @@ | ||
# Build the manager binary | ||
FROM golang:1.23 AS builder | ||
ARG TARGETOS | ||
ARG TARGETARCH | ||
FROM --platform=${BUILDPLATFORM} golang:1.23 AS builder | ||
|
||
WORKDIR /workspace | ||
# Copy the Go Modules manifests | ||
|
||
# copy the Go Modules manifests individually to improve container build caching (see | ||
# go mod download step below) | ||
COPY go.mod go.mod | ||
COPY go.sum go.sum | ||
|
||
# This particular COPY needs to be executed before go mod download since it is referenced by a replace directive | ||
# in go.mod. | ||
COPY images/pkg/common/ images/pkg/common/ | ||
|
||
# cache deps before building and copying source so that we don't need to re-download as much | ||
# and so that source changes don't invalidate our downloaded layer | ||
# download dependencies before building and copying the sources, so that we don't need to re-download as much | ||
# and so that source changes do not invalidate the cached container image build layer | ||
RUN go mod download | ||
|
||
# Copy the go source | ||
COPY cmd/ cmd/ | ||
COPY api/ api/ | ||
COPY internal/ internal/ | ||
|
||
# Build | ||
# the GOARCH has not a default value to allow the binary be built according to the host where the command | ||
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO | ||
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore, | ||
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform. | ||
ARG TARGETOS | ||
ARG TARGETARCH | ||
|
||
# Note: By using --platform=${BUILDPLATFORM} in the FROM statement for the build stage, we run the cross-platform | ||
# compilation (specifically, cross-CPU-architecture compilation) for the multi-platform image on the platform/CPU | ||
# architecture of the machine running the docker build, instead of running the build via emulation (QEMU); go build | ||
# takes care of producing the correct binary for the target architecture on its own. | ||
# See https://www.docker.com/blog/faster-multi-platform-builds-dockerfile-cross-compilation-guide/. | ||
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -v -a -o manager cmd/main.go | ||
|
||
# Use distroless as minimal base image to package the manager binary | ||
# Refer to https://github.com/GoogleContainerTools/distroless for more details | ||
FROM gcr.io/distroless/static:nonroot | ||
WORKDIR / | ||
COPY --from=builder /workspace/manager . | ||
USER 65532:65532 | ||
|
||
ENTRYPOINT ["/manager"] | ||
ENTRYPOINT ["/manager"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters