Platform-specific RUN statements in Dockerfile

Multi-arch images can be built using the built-in variables of buildkit

https://docs.docker.com/engine/reference/builder/#automatic-platform-args-in-the-global-scope

  • TARGETPLATFORM - platform of the build result. Eg linux/amd64, linux/arm/v7, windows/amd64.
  • TARGETOS - OS component of TARGETPLATFORM
  • TARGETARCH - architecture component of TARGETPLATFORM
  • TARGETVARIANT - variant component of TARGETPLATFORM
  • BUILDPLATFORM - platform of the node performing the build.
  • BUILDOS - OS component of BUILDPLATFORM
  • BUILDARCH - architecture component of BUILDPLATFORM
  • BUILDVARIANT - variant component of BUILDPLATFORM

You can’t compose the Dockerfile dynamically unless you are using a template system and generate the file, but you don’t need that. Use the variables in a shell script and write the conditions there or just create multiple scripts containing the architecture in the name like install-arm64.sh and install-amd64.sh and execute the script like install-${TARGETARCH}.sh.

1 Like