Skip to content

Instantly share code, notes, and snippets.

@developer-guy
Created November 23, 2020 07:29
Show Gist options
  • Save developer-guy/9978d7d2a1412664571f22f63063021d to your computer and use it in GitHub Desktop.
Save developer-guy/9978d7d2a1412664571f22f63063021d to your computer and use it in GitHub Desktop.
cross-build
$ cat Makefile
cross-build:
@docker buildx create --name mybuilder --use
@docker buildx build --platform ${BUILDX_PLATFORMS} -t ${PROD_IMAGE} --push ./app
$ BUILDX_BINARY_URL="https://github.com/docker/buildx/releases/download/v0.4.2/buildx-v0.4.2.linux-amd64"
$ curl --output docker-buildx \
--silent --show-error --location --fail --retry 3 \
"$BUILDX_BINARY_URL"
$ mkdir -p ~/.docker/cli-plugins
$ mv docker-buildx ~/.docker/cli-plugins/
$ chmod a+x ~/.docker/cli-plugins/docker-buildx
$ docker buildx install
$ # Run binfmt
$ docker run --rm --privileged tonistiigi/binfmt:latest --install "$BUILDX_PLATFORMS"
$ export BUILDX_PLATFORMS="linux/amd64,linux/arm64,linux/ppc64le,linux/s390x,linux/386,linux/arm/v7,linux/arm/v6"
$ BUILDX_PLATFORMS="$BUILDX_PLATFORMS" make cross-build
@developer-guy
Copy link
Author

developer-guy commented Nov 23, 2020

We can build multi-platform images using three different strategies that are supported by Buildx and Dockerfiles:

  1. Using the QEMU emulation support in the kernel.In particular, this approach is considered to be the most suitable approach when one is working on a Docker Desktop ( usually Mac or Windows ).
  2. Building on multiple native nodes using the same builder instance.This approach is suitable for covering all the use-cases that are not handled efficiently by QEMU approach, and as a result we achieve better performance.
  3. Using a stage in Dockerfile to cross-compile to different architectures.In this case, multi-stage builds in Dockerfiles can be effectively used to build binaries for the platform specified with --platform using the native architecture of the build node. A list of build arguments like BUILDPLATFORM and TARGETPLATFORM is available automatically inside your Dockerfile.

Link: https://blog.knoldus.com/a-word-on-docker-buildx/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment