WORKDIR /work
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
apt-get update && apt-get install -y \
build-essential \
cmake \
curl \
gettext \
ninja-build \
unzip
ADD https://github.com/neovim/neovim.git#stable .
RUN make CMAKE_BUILD_TYPE=RelWithDebInfo
FROM scratch
COPY --from=build /work/build/bin/nvim /
```
3. Build the image for `linux/amd64` and `linux/arm64` using Docker Build Cloud:
```console
$ docker build \
--builder <cloud-builder> \
--platform linux/amd64,linux/arm64 \
--output ./bin .
```
This command builds the image using the cloud builder and exports the
binaries to the `bin` directory.
4. Verify that the binaries are built for both platforms. You should see the
`nvim` binary for both `linux/amd64` and `linux/arm64`.
```console
$ tree ./bin
./bin
├── linux_amd64
│ └── nvim
└── linux_arm64
└── nvim
3 directories, 2 files
```
### Cross-compiling a Go application
This example demonstrates how to cross-compile a Go application for multiple
platforms using multi-stage builds. The application is a simple HTTP server
that listens on port 8080 and returns the architecture of the container.
This example uses Go, but the same principles apply to other programming
languages that support cross-compilation.
Cross-compilation with Docker builds works by leveraging a series of
pre-defined (in BuildKit) build arguments that give you information about
platforms of the builder and the build targets. You can use these pre-defined
arguments to pass the platform information to the compiler.
In Go, you can use the `GOOS` and `GOARCH` environment variables to specify the
target platform to build for.
Prerequisites:
- Docker Desktop or Docker Engine
Steps:
1. Create an empty directory and navigate to it:
```console
$ mkdir go-server
$ cd go-server
```
2. Create a base Dockerfile that builds the Go application:
```dockerfile
# syntax=docker/dockerfile:1
FROM golang:alpine AS build
WORKDIR /app
ADD https://github.com/dvdksn/buildme.git#eb6279e0ad8a10003718656c6867539bd9426ad8 .
RUN go build -o server .
FROM alpine
COPY --from=build /app/server /server
ENTRYPOINT ["/server"]
```
This Dockerfile can't build multi-platform with cross-compilation yet. If
you were to try to build this Dockerfile with `docker build`, the builder
would attempt to use emulation to build the image for the specified
platforms.
3. To add cross-compilation support, update the Dockerfile to use the
pre-defined `BUILDPLATFORM` and `TARGETPLATFORM` build arguments. These