Building a native image on Mac ARM64 for linux/amd64 in sbt

1.1k Views Asked by At

I'm using the sbt-native-packager plugin in sbt to generate a linux executable to be wrapped inside an alpine docker image.

Since I've switches to the new Mac arch (ARM64) this doesn't work anymore.

Using this config, the builder will default select the linux/arm64 manifest

 GraalVMNativeImage / containerBuildImage := GraalVMNativeImagePlugin
    .generateContainerBuildImage(s"ghcr.io/graalvm/graalvm-ce:ol8-java17-22")

The executable I create is of the type

app: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, for GNU/Linux 3.7.0, BuildID[sha1]=52b76d5e755b56293ae87fc6899655312ce4004c, with debug_info, not stripped

Trying to run this from alpine docker image will result in

standard_init_linux.go:228: exec user process caused: no such file or directory

or trying to run the file via sh:

/bin/sh: app: not found

The thing is.

I want to just build the linux/amd64 image and wrap this in a alpine image, from my macbook

Is there any way to do this?

I've tried referencing the correct digest directly

GraalVMNativeImage / containerBuildImage := GraalVMNativeImagePlugin
.generateContainerBuildImage("ghcr.io/graalvm/graalvm-ce@sha256:c2dbf1352ae7ce939dc52e0e4645d905a9366c5ea0ff5f12ef08b57d48ae847a")

But I think I will settle for getting the ELF 64-bit LSB executable, ARM aarch64, app running in a light weight docker image for now

1

There are 1 best solutions below

0
mike On

I know nothing about Graal but if you use the following code in build.sbt you'll be able to build amd64 images from a Mac M1 (arm64):

dockerBuildCommand := {
   if (sys.props("os.arch") != "amd64") {
     // use buildx with platform to build supported amd64 images on other CPU architectures
     // this may require that you have first run 'docker buildx create' to set docker buildx up
     dockerExecCommand.value ++ Seq("buildx", "build", "--platform=linux/amd64", "--load") ++ dockerBuildOptions.value :+ "."
   } else dockerBuildCommand.value
 }

Source https://github.com/sbt/sbt-native-packager/issues/1503