From aaa3765e1de70e3d55c2f025d148732f3a4fd960 Mon Sep 17 00:00:00 2001 From: Alexander Bachmann Date: Tue, 12 Nov 2024 00:11:41 +0100 Subject: [PATCH] hack: use buildx to cross-build images with docker When Docker is used and the target architecture differs from the host architecture, buildx is used for cross-building. Depending on the container engine and the target architecture, multiple commands need to be executed to prepare the environment, build the image, and clean up artifacts afterward. To streamline this process, a tasks array (an array of lambdas) has been introduced, its elements are executed sequencally at the end of the container_build function. Signed-off-by: Alexander Bachmann --- hack/build-image | 53 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/hack/build-image b/hack/build-image index 5f8251a..dc2acd5 100755 --- a/hack/build-image +++ b/hack/build-image @@ -203,23 +203,42 @@ def container_engine(cli): def container_build(cli, target): """Construct and execute a command to build the target container image.""" - args = [container_engine(cli), "build"] - pkgs_from = PACKAGES_FROM[target.pkg_source] - if pkgs_from: - args.append(f"--build-arg=INSTALL_PACKAGES_FROM={pkgs_from}") - # docker doesn't currently support alt. architectures - if "docker" in args[0]: - if target.arch != host_arch(): - raise RuntimeError("Docker does not support --arch") - elif target.arch != host_arch() or FORCE_ARCH_FLAG: - # We've noticed a few small quirks when using podman with the --arch - # option. The main issue is that building the client image works - # but then the toolbox image fails because it somehow doesn't see - # the image we just built as usable. This doesn't happen when - # --arch is not provided. So if the target arch and the host_arch - # are the same, skip passing the extra argument. - args.append(f"--arch={target.arch}") - run(cli, args + create_common_container_engine_args(cli, target), check=True) + eng = container_engine(cli) + tasks = [] + + # For docker cross-builds we need to use buildx + if "docker" in eng and target.arch != host_arch(): + args = [eng, "buildx"] + + # Docker's default builder only supports the host architecture. + # Therefore, we need to create a new builder to support other + # architectures, and we must ensure we start with a fresh builder + # that does not contain any images from previous builds. + tasks.append(lambda : run(cli, args + ["rm", target.flat_name()], check=False)) + tasks.append(lambda : run(cli, args + ["create", f"--name={target.flat_name()}"], check=True)) + + tasks.append(lambda : run(cli, args + [ + "build", + f"--builder={target.flat_name()}", + f"--platform=linux/{target.arch}", + "--load"] + create_common_container_engine_args(cli, target), check=True)) + + tasks.append(lambda : run(cli, args + ["rm", target.flat_name()], check=True)) + else: + args = [eng, "build"] + if target.arch != host_arch() or FORCE_ARCH_FLAG: + # We've noticed a few small quirks when using podman with the --arch + # option. The main issue is that building the client image works + # but then the toolbox image fails because it somehow doesn't see + # the image we just built as usable. This doesn't happen when + # --arch is not provided. So if the target arch and the host_arch + # are the same, skip passing the extra argument. + args += [f"--arch={target.arch}"] + + tasks.append(lambda : run(cli, args + create_common_container_engine_args(cli, target), check=True)) + + for task in tasks: + task() def create_common_container_engine_args(cli, target): args = []