Skip to content

Commit 028a249

Browse files
committed
hack: use buildx to cross-build images with docker
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]>
1 parent a5244a3 commit 028a249

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

hack/build-image

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ QUAL_FQIN = 'fqin'
125125

126126

127127
_DISCOVERED_CONTAINER_ENGINES = []
128+
_BUILDX_BUILDER_NAME = "samba-in-kubernetes"
128129

129130

130131
def check_kind(kind):
@@ -203,14 +204,25 @@ def container_engine(cli):
203204

204205
def container_build(cli, target):
205206
"""Construct and execute a command to build the target container image."""
206-
args = [container_engine(cli), "build"]
207-
pkgs_from = PACKAGES_FROM[target.pkg_source]
208-
if pkgs_from:
209-
args.append(f"--build-arg=INSTALL_PACKAGES_FROM={pkgs_from}")
210-
# docker doesn't currently support alt. architectures
207+
eng = container_engine(cli)
208+
args = [eng, "build"]
209+
211210
if "docker" in args[0]:
211+
# if the target arch and the host_arch are not the same, we need to use buildx
212212
if target.arch != host_arch():
213-
raise RuntimeError("Docker does not support --arch")
213+
args = [
214+
eng,
215+
"buildx",
216+
"build",
217+
f"--builder={_BUILDX_BUILDER_NAME}",
218+
f"--platform=linux/{target.arch}",
219+
"--load"
220+
]
221+
# Docker's default builder only supports the host architecture.
222+
# Therefore, we need to create a new builder to support other
223+
# architectures. Errors are suppressed to prevent issues when
224+
# the builder is already available - this can be improved later.
225+
run(cli, [eng, "buildx", "create", f"--name={_BUILDX_BUILDER_NAME}"], check=False)
214226
elif target.arch != host_arch() or FORCE_ARCH_FLAG:
215227
# We've noticed a few small quirks when using podman with the --arch
216228
# option. The main issue is that building the client image works
@@ -219,6 +231,11 @@ def container_build(cli, target):
219231
# --arch is not provided. So if the target arch and the host_arch
220232
# are the same, skip passing the extra argument.
221233
args.append(f"--arch={target.arch}")
234+
235+
pkgs_from = PACKAGES_FROM[target.pkg_source]
236+
if pkgs_from:
237+
args.append(f"--build-arg=INSTALL_PACKAGES_FROM={pkgs_from}")
238+
222239
if cli.extra_build_arg:
223240
args.extend(cli.extra_build_arg)
224241
for tname in target.all_names(baseless=cli.without_repo_bases):

0 commit comments

Comments
 (0)