Skip to content

Commit

Permalink
chore: improve container image build times
Browse files Browse the repository at this point in the history
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
basti1302 committed Feb 15, 2025
1 parent aec3fed commit 9bb9fc0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 29 deletions.
30 changes: 15 additions & 15 deletions Dockerfile
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"]
7 changes: 5 additions & 2 deletions images/collector/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.23-alpine AS builder
FROM --platform=${BUILDPLATFORM} golang:1.23-alpine AS builder

RUN apk add --update git make yq && apk cache clean

Expand All @@ -9,7 +9,10 @@ RUN export builder_version=$(yq '.connectors[0].gomod | match(" v(\d+\.\d+\.\d+)
&& echo "Using OpenTelemetry collector builder version v${builder_version}." \
&& go install go.opentelemetry.io/collector/cmd/builder@v${builder_version}

RUN CGO_ENABLED=0 builder --config=builder/config.yaml \
ARG TARGETOS
ARG TARGETARCH

RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} builder --config=builder/config.yaml \
&& chmod u+x /src/dist/dash0-operator-collector

FROM alpine:3.20.2
Expand Down
26 changes: 20 additions & 6 deletions images/configreloader/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
# Note: Expects dash0-operator/images as the build context.
FROM golang:alpine AS builder
COPY configreloader/src /usr/local/go/src/configreloader
COPY pkg /usr/local/go/pkg
# Note: This Dockerfile expects dash0-operator/images as the build context.

FROM --platform=${BUILDPLATFORM} golang:1.24-alpine3.21 AS builder

WORKDIR /usr/local/go/src/configreloader
RUN CGO_ENABLED=0 go build -ldflags '-extldflags "-static"' configreloader

# Copy go.mod and friends and run go mod download separately before starting the container image build, to improve
# container build caching.
COPY configreloader/src/go.mod /usr/local/go/src/configreloader/go.mod
COPY configreloader/src/go.sum /usr/local/go/src/configreloader/go.sum
COPY pkg /usr/local/go/pkg
RUN go mod download

# now copy the actual go sources and compile them
COPY configreloader/src/*.go /usr/local/go/src/configreloader

ARG TARGETOS
ARG TARGETARCH

RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -ldflags '-extldflags "-static"' -v configreloader

FROM alpine:3.20.2 AS certs
RUN apk --update add ca-certificates && apk cache clean
Expand All @@ -12,4 +26,4 @@ FROM scratch
COPY --from=builder /usr/local/go/src/configreloader/configreloader /app/configreloader
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
USER 65532:65532
ENTRYPOINT [ "/app/configreloader" ]
ENTRYPOINT [ "/app/configreloader" ]
26 changes: 20 additions & 6 deletions images/filelogoffsetsynch/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
# Note: Expects dash0-operator/images as the build context.
FROM golang:alpine AS builder
COPY filelogoffsetsynch/src /usr/local/go/src/filelogoffsetsynch
COPY pkg /usr/local/go/pkg
# Note: This Dockerfile expects dash0-operator/images as the build context.

FROM --platform=${BUILDPLATFORM} golang:1.24-alpine3.21 AS builder

WORKDIR /usr/local/go/src/filelogoffsetsynch
RUN CGO_ENABLED=0 go build -ldflags '-extldflags "-static"' filelogoffsetsynch

# Copy go.mod and friends and run go mod download separately before starting the container image build, to improve
# container build caching.
COPY filelogoffsetsynch/src/go.mod /usr/local/go/src/filelogoffsetsynch/go.mod
COPY filelogoffsetsynch/src/go.sum /usr/local/go/src/filelogoffsetsynch/go.sum
COPY pkg /usr/local/go/pkg
RUN go mod download

# now copy the actual go sources and compile them
COPY filelogoffsetsynch/src/*.go /usr/local/go/src/filelogoffsetsynch

ARG TARGETOS
ARG TARGETARCH

RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -ldflags '-extldflags "-static"' -v filelogoffsetsynch

FROM alpine:3.20.2 AS certs
RUN apk --update add ca-certificates && apk cache clean
Expand All @@ -12,4 +26,4 @@ FROM scratch
COPY --from=builder /usr/local/go/src/filelogoffsetsynch/filelogoffsetsynch /app/filelogoffsetsynch
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
USER 65532:65532
ENTRYPOINT [ "/app/filelogoffsetsynch" ]
ENTRYPOINT [ "/app/filelogoffsetsynch" ]

0 comments on commit 9bb9fc0

Please sign in to comment.