Skip to content

Commit

Permalink
hack: use buildx to cross-build images with docker
Browse files Browse the repository at this point in the history
When Docker is used and the target architecture differs from the host
architecture, use buildx for cross-building.

Signed-off-by: Alexander Bachmann <[email protected]>
  • Loading branch information
abachmann committed Oct 22, 2024
1 parent a5244a3 commit 028a249
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions hack/build-image
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ QUAL_FQIN = 'fqin'


_DISCOVERED_CONTAINER_ENGINES = []
_BUILDX_BUILDER_NAME = "samba-in-kubernetes"


def check_kind(kind):
Expand Down Expand Up @@ -203,14 +204,25 @@ 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
eng = container_engine(cli)
args = [eng, "build"]

if "docker" in args[0]:
# if the target arch and the host_arch are not the same, we need to use buildx
if target.arch != host_arch():
raise RuntimeError("Docker does not support --arch")
args = [
eng,
"buildx",
"build",
f"--builder={_BUILDX_BUILDER_NAME}",
f"--platform=linux/{target.arch}",
"--load"
]
# Docker's default builder only supports the host architecture.
# Therefore, we need to create a new builder to support other
# architectures. Errors are suppressed to prevent issues when
# the builder is already available - this can be improved later.
run(cli, [eng, "buildx", "create", f"--name={_BUILDX_BUILDER_NAME}"], check=False)
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
Expand All @@ -219,6 +231,11 @@ def container_build(cli, target):
# --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}")

pkgs_from = PACKAGES_FROM[target.pkg_source]
if pkgs_from:
args.append(f"--build-arg=INSTALL_PACKAGES_FROM={pkgs_from}")

if cli.extra_build_arg:
args.extend(cli.extra_build_arg)
for tname in target.all_names(baseless=cli.without_repo_bases):
Expand Down

0 comments on commit 028a249

Please sign in to comment.