Multi-platform builds

Intermediate Docker

Mike Metzger

Data Engineering Consultant

Multi-platform?

  • What does multi-platform mean?
    • Different OS types
      • linux
      • windows
      • macos
    • Different CPU types
      • x64_64 or amd64
      • arm64
      • arm7
  • Usually referred to as os/cpu, such as linux/amd64

Linux Tux logo

AMD64 logo

ARM logo

Intermediate Docker

Creating multi-platform builds

  • Is built on multi-stage build behavior
  • The initial / build stage tends to use cross-compilers and relies on the architecture of the host system
  • Final stage uses the architecture / OS for the intended target.
Intermediate Docker

Multi-platform Dockerfile options

  • Build stage uses the --platform=$BUILDPLATFORM flag
    • $BUILDPLATFORM represents the platform of the host running the build
  • Sometimes uses the ARG directive
    • Passes local environment variables into the Docker build system
    • In this case, TARGETOS and TARGETARCH
    • ARG TARGETOS TARGETARCH
    • The environment variables at the host level can be defined previously or using the env command.
Intermediate Docker

Multi-platform example

# Initial stage, using local platform
FROM --platform=$BUILDPLATFORM golang:1.21 AS build

# Copy source into place WORKDIR /src COPY . .
# Pull the environment variables from the host ARG TARGETOS TARGETARCH
# Compile code using the ARG variables RUN env GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /final/app .
# Create container and load the cross-compiled code FROM alpine COPY --from=build /final/app /bin
Intermediate Docker

Building a multi-platform build

  • To create a multi-platform build, instead of using docker build, we must use docker buildx with assorted options
  • docker buildx provides more commands and capabilities over docker build, including the option to specify a platform

docker buildx build --platform linux/amd64,linux/arm64 -t multi-platform-app .

  • Prior to running the build, we must also have a new builder container present. This is done with the docker buildx create --bootstrap --use command.
Intermediate Docker

Let's practice!

Intermediate Docker

Preparing Video For Download...