Skip to content

Instantly share code, notes, and snippets.

@imjasonh
Last active December 3, 2021 23:17
Show Gist options
  • Save imjasonh/22b59abb1fc2ab99f76b46199c486168 to your computer and use it in GitHub Desktop.
Save imjasonh/22b59abb1fc2ab99f76b46199c486168 to your computer and use it in GitHub Desktop.
Reproducing multi-arch Go build performance in `docker buildx` vs `ko`
defaultBaseImage: gcr.io/imjasonh/combined

Reproducing multi-arch Go build performance in docker buildx vs ko

https://www.docker.com/blog/faster-multi-platform-builds-dockerfile-cross-compilation-guide/

Setup

  • clone https://github.com/google/ko

  • for buildx, add the Dockerfile

    • this is the "fully-optimized cross-compiling Go Dockerfile" from the above article, lightly adapted for the ko codebase
    • no go mod download because ko repo uses vendor/
  • for ko, add the .ko.yaml

    • this defines a base image that consists of Distroless + Windows Nanoserver, for multi-arch and multi-OS builds
  • for my tests, I'm on a 2017 MBP

    • 2.8 GHz Quad-Core Intel Core i7
    • 16 GB 2133 MHz LPDDR3
    • macOS 11.6.1 (20G224)

buildx

docker buildx build . --push -t gcr.io/imjasonh/ko-buildx \
  --platform=linux/amd64,linux/arm,linux/arm64,linux/ppc64le,linux/s390x
  • NB: Windows builds didn't work at all for me, so I just excluded them
  • cold cache: 208s
  • code change: 32s
  • no-op: 8s
  • image: gcr.io/imjasonh/ko-buildx@sha256:f112526706bd3197a594c45161f2a854741a8e21a73e8c88185f6674e004e9e5

ko

ko publish --platform=all ./
  • cold cache: 211s
  • code change: 91s
  • no-op: 59s
  • image: gcr.io/imjasonh/ko-ko/ko-98b8c7facdad74510a7cae0cd368eb4e@sha256:4d9166339f254f11b879257a7ebb5f5fa2cfa9c3d15f27ebb853c28c8d52d5be
FROM --platform=$BUILDPLATFORM golang:1.17-alpine AS build
WORKDIR /src
COPY go.mod go.sum .
COPY . .
ARG TARGETOS TARGETARCH
RUN --mount=target=. \
--mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /out/myapp .
FROM gcr.io/imjasonh/combined
COPY --from=build /out/myapp /bin
ENTRYPOINT ["/bin/myapp"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment