Build linux/arm64 docker image on linux/amd64 host
Asked Answered
B

1

13

I am running a Jenkins Alpine Linux AMD64 docker image, which I'm building myself and want to add linux/arm64 docker buildx support to it, in order to generate multi-platform images and I do not know how it supposed to work.

When I check the supported platform I get:

+ docker buildx ls
NAME/NODE DRIVER/ENDPOINT STATUS  PLATFORMS
default * docker                  
  default default         running linux/amd64, linux/386

since I'm within an AMD64 image. I read that I need to install qemu for this, but I have no clue how buildx will recognize that.

The documentation is relatively bare on this at: https://docs.docker.com/buildx/working-with-buildx/

Anyone an idea how to add linux/arm64 build capability within a linux/amd64 image?

The only solution I see right now is to build an image on an actual arm64 system.

Benefice answered 18/1, 2022 at 15:2 Comment(3)
Does this help? https://mcmap.net/q/400226/-quot-exec-format-error-quot-when-running-containers-build-with-apple-m1-chip-arm-based-systemsParadrop
Thanks, I tried also using buildx and setting the platform, but I can't build an ARM64 image on AMD64 for some reason.Benefice
Interesting, not sure if this is a limitation of Docker. An alternative (not sure if suitable in your case) is to build the image with a CI/CD tool (Github actions) where it is more flexibleParadrop
R
26

To use buildx, make sure your Docker runtime is at least version 19.03. buildx actually comes bundled with Docker by default, but needs to be enabled by setting the environment variable DOCKER_CLI_EXPERIMENTAL.

export DOCKER_CLI_EXPERIMENTAL=enabled

If you're on Linux, you need to set up binfmt_misc. This is pretty easy in most distributions, but is even easier now that you can just run a privileged Docker container to set it up for you.

docker run --rm --privileged docker/binfmt:66f9012c56a8316f9244ffd7622d7c21c1f6f28d

Create a new builder which gives access to the new multi-architecture features:

docker buildx create --use --name multi-arch-builder

Then you'll be able to build the containers with:

docker buildx build --platform=[your target platform] ...

This is the setup I use on my Jenkins pipeline.

Relevant documentation:

Rager answered 24/1, 2022 at 16:19 Comment(2)
docker run --rm --privileged docker/binfmt:66f9012c56a8316f9244ffd7622d7c21c1f6f28d doesn't work anymore. But I found that docker buildx create --use --name multi-arch-builder was the only command I had to run. After that buildx worked!Woodsum
Is there anyway I can do the same thing with jenkins DIND container where host just have containerd?Covariance

© 2022 - 2024 — McMap. All rights reserved.