Skip to content

Commit 0978f23

Browse files
committed
hack: use buildx to cross build images with docker
1 parent e832f6b commit 0978f23

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

hack/build-image

Lines changed: 16 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,18 @@ 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 = [eng, "buildx", "build", f"--builder={_BUILDX_BUILDER_NAME}", f"--platform=linux/{target.arch}"]
214+
# Docker's default builder only supports the host architecture.
215+
# Therefore, we need to create a new builder to support other
216+
# architectures. Errors are suppressed to prevent issues when
217+
# the builder is already available - this can be improved later.
218+
run(cli, [eng, "buildx", "create", f"--name={_BUILDX_BUILDER_NAME}"], check=False)
214219
elif target.arch != host_arch() or FORCE_ARCH_FLAG:
215220
# We've noticed a few small quirks when using podman with the --arch
216221
# option. The main issue is that building the client image works
@@ -219,6 +224,11 @@ def container_build(cli, target):
219224
# --arch is not provided. So if the target arch and the host_arch
220225
# are the same, skip passing the extra argument.
221226
args.append(f"--arch={target.arch}")
227+
228+
pkgs_from = PACKAGES_FROM[target.pkg_source]
229+
if pkgs_from:
230+
args.append(f"--build-arg=INSTALL_PACKAGES_FROM={pkgs_from}")
231+
222232
if cli.extra_build_arg:
223233
args.extend(cli.extra_build_arg)
224234
for tname in target.all_names(baseless=cli.without_repo_bases):

0 commit comments

Comments
 (0)