Multi-platform builds
Earthly has the ability to perform builds for multiple platforms, in parallel. This page walks through setting up your system to support emulation as well as through a few simple examples of how to use this feature.
Currently only linux is supported as the build platform OS. Building with Windows containers will be available in a future version of Earthly.
By default, builds are performed on the same processor architecture as available on the host natively. Using the --platform flag across various Earthfile commands or as part of the earthly command, it is possible to override the build platform and thus be able to execute builds on non-native processor architectures. Execution of non-native binaries can be performed via QEMU emulation.
In some cases, execution of the build itself does not need to happen on the target architecture, through cross-compilation features of the compiler. Examples of languages that support cross-compilation are Go and Rust. This approach may be more beneficial in many cases, as there is no need to install QEMU and also, the build is more performant.

Prerequisites for emulation

In order to execute emulated build steps (usually RUN), QEMU needs to be installed and set up. This will allow you perform Earthly builds on non-native platforms, but also incidentally, to run Docker images on your host system through docker run --platform=....

Windows and Mac

On Mac and on Windows, the Docker Desktop app comes with QEMU readily installed and ready to go, so no special consideration is necessary.

Linux

On Linux, QEMU needs to be installed manually. On Ubuntu, this can be achieved by running:
1
sudo apt-get install qemu binfmt-support qemu-user-static
2
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
3
docker stop earthly-buildkitd || true
Copied!
The docker run command above enables execution of different multi-architecture containers by QEMU and binfmt_misc. It only needs to be run once.

GitHub Actions

To make use of emulation in GitHub Actions, the following step needs to be included in every job that performs a multi-platform build:
1
jobs:
2
<job-name>:
3
steps:
4
-
5
name: Set up QEMU
6
id: qemu
7
uses: docker/setup-qemu-[email protected]
8
with:
9
image: tonistiigi/binfmt:latest
10
platforms: all
11
- uses: actions/[email protected]
12
- ...
Copied!

Performing multi-platform builds

In order to execute builds for multiple platforms, the execution may be parallelized through the repeated use of the BUILD --platform flag. For example:
1
build-all-platforms:
2
BUILD --platform=linux/amd64 --platform=linux/arm/v7 +build
3
​
4
build:
5
...
Copied!
If the +build target were invoked without the use of any flag, Earthly would simply perform the build on the native architecture of the host system.
However, invoking the target +build-all-platforms causes +build to execute twice, in parallel: one time on linux/amd64 and another time on linux/arm/v7.
You may also override the target platform when issuing the earthly build command. For example:
1
earthly --platform=linux/arm64 +build
Copied!
This would cause the build to execute on the linux/arm64 architecture.

Saving multi-platform images

The easiest way to include platform information as part of a build is through the use of FROM --platform. For example:
1
FROM --platform=linux/arm/v7 alpine:3.13
Copied!
If multiple targets create an image with the same name, but for different platforms, the images will be merged into a multi-platform image during export. For example:
1
build-all-platforms:
2
BUILD +build-amd64
3
BUILD +build-arm-v7
4
​
5
build-amd64:
6
FROM --platform=linux/amd64 alpine:3.13
7
...
8
SAVE IMAGE --push org/myimage:latest
9
​
10
build-arm-v7:
11
FROM --platform=linux/arm/v7 alpine:3.13
12
...
13
SAVE IMAGE --push org/myimage:latest
Copied!
When earthly --push +build-all-platforms is executed, the build will push a multi-manifest image to the Docker registry. The manifest will contain two images: one for linux/amd64 and one for linux/arm/v7. This works as such because both targets that save images use the exact same Docker tag for the image.
Of course, in some situations, the build steps are the same (except they run on different platform), so the two definitions can be merged like so:
1
build-all-platforms:
2
BUILD --platform=linux/amd64 --platform=linux/arm/v7 +build
3
​
4
build:
5
FROM alpine:3.13
6
...
7
SAVE IMAGE --push org/myimage:latest
Copied!
A more complete version of this example is available in examples/multiplatform in GitHub. You may try out this example without cloning by running
1
earthly github.com/earthly/earthly/examples/multiplatform:main+all
2
docker run --rm earthly/examples:multiplatform
3
docker run --rm earthly/examples:multiplatform_linux_amd64
4
docker run --rm earthly/examples:multiplatform_linux_arm_v7
Copied!
Note
As of the time of writing this article, the docker CLI has limited support for working with multi-manifest images locally. For this reason, when exporting an image to the local Docker daemon, Earthly provides the different architectures as different Docker tags.
For example, the above build would yield locally:
  • org/myimage:latest
  • org/myimage:latest_linux_amd64 (the same as org/myimage:latest if running on a linux/amd64 host)
  • org/myimage:latest_linux_arm_v7
The additional Docker tags are only available for use on the local system. When pushing an image to a Docker registry, it is pushed as a single multi-manifest image.

Creating multi-platform images without emulation

Building multi-platform images does not necessarily require that execution of the build itself takes place on the target platform. Through the use of cross-compilation, it is possible to obtain target-platform binaries compiled on the host-native platform. At the end, these binaries may be placed in a final image which is marked for a specific platform.
Note, however, that not all programming languages have support for cross-compilation. The applicability of this approach may be limited as a result. Examples of languages that can cross-compile for other platforms are Go and Rust.
Here is an example where a multi-platform image can be created without actually executing any RUN on the target platform (and therefore emulation is not necessary):
1
build-all-platforms:
2
BUILD +build-amd64
3
BUILD +build-arm-v7
4
​
5
build:
6
FROM golang:1.15-alpine3.13
7
WORKDIR /example
8
ARG GOOS=linux
9
ARG GOARCH=amd64
10
ARG GOARM
11
COPY main.go ./
12
RUN go build -o main main.go
13
SAVE ARTIFACT ./main
14
​
15
build-amd64:
16
FROM --platform=linux/amd64 alpine:3.13
17
COPY +build/main ./example/main
18
ENTRYPOINT ["/example/main"]
19
SAVE IMAGE --push org/myimage:latest
20
​
21
build-arm-v7:
22
FROM --platform=linux/arm/v7 alpine:3.13
23
COPY \
24
--platform=linux/amd64 \
25
(+build/main --GOARCH=arm --GOARM=v7) ./example/main
26
ENTRYPOINT ["/example/main"]
27
SAVE IMAGE --push org/myimage:latest
Copied!
The key here is the use of the COPY commands. The execution of the target +build may take place on the host platform (in this case, linux/amd64) and yet produce binaries for either amd64 or arm/v7. Since there is no RUN command as part of the +build-arm-v7 target, no emulation is necessary.

Making use of builtin platform args

A number of builtin build args are made available to be used in conjunction with multi-platform builds:
  • TARGETPLATFORM (eg linux/arm/v7)
  • TARGETOS (eg linux)
  • TARGETARCH (eg arm)
  • TARGETVARIANT (eg v7)
Here is an example of how the build described above could be simplified through the use of these build args:
1
build-all-platforms:
2
BUILD --platform=linux/amd64 --platform=linux/arm/v7 +build-image
3
​
4
build:
5
FROM golang:1.15-alpine3.13
6
WORKDIR /example
7
ARG GOOS=linux
8
ARG GOARCH=amd64
9
ARG VARIANT
10
COPY main.go ./
11
RUN GOARM=${VARIANT#"v"} go build -o main main.go
12
SAVE ARTIFACT ./main
13
​
14
build-image:
15
ARG TARGETPLATFORM
16
ARG TARGETARCH
17
ARG TARGETVARIANT
18
FROM --platform=$TARGETPLATFORM alpine:3.13
19
COPY \
20
--platform=linux/amd64 \
21
(+build/main --GOARCH=$TARGETARCH --VARIANT=$TARGETVARIANT) ./example/main
22
ENTRYPOINT ["/example/main"]
23
SAVE IMAGE --push org/myimage:latest
Copied!
The code of this example is available in examples/multiplatform-cross-compile in GitHub. You may try out this example without cloning by running
1
earthly github.com/earthly/earthly/examples/multiplatform-cross-compile:main+build-all-platforms
Copied!

USER platform args

Additional USER builtin build args can be used to determine the architecture of the host that called earthly. This can be useful to determine if cross-platform emulation was used.
  • USERPLATFORM (eg linux/amd64)
  • USEROS (eg linux)
  • USERARCH (eg amd64)
  • USERVARIANT (eg ``; an empty string for non-arm platforms)

Emulation and WITH DOCKER

Please note that WITH DOCKER has an important limitation for cross-platform builds: the target containing WITH DOCKER needs to be executing on the native architecture of the host system. The images being run within WITH DOCKER can be of any architecture, however.
In other words, the following will NOT work on amd64:
1
build:
2
FROM --platform=linux/arm64 earthly/dind
3
WITH DOCKER --pull=earthly/examples:multiplatform
4
RUN docker run earthly/examples:multiplatform
5
END
Copied!
However, the following will:
1
build:
2
FROM earthly/dind
3
WITH DOCKER --pull=earthly/examples:multiplatform
4
RUN docker run --platform=linux/arm64 earthly/examples:multiplatform
5
END
Copied!
The reason for this is that behind the scenes WITH DOCKER starts up an isolated Docker daemon running within a container, and docker-in-docker is not yet supported in a QEMU environment.
Last modified 2d ago