Docker Community Forums

Share and learn in the Docker community.

Building FROM scratch for cross compilation using buildx

Okay, so this is a complicated topic, so a thanks to anyone who actually takes the time to read this. This all started by trying to create an executable from a python script to run on target arch.

The target arch is arm64. I am doing all of this on a MAC. The major gotcha is that the target device uses uclibc. if it used glibc or musl I would be able to cross compile using the ubuntu container described below or an alpine container with python. (using pyinstaller to create executable)

I created a buildx container and ran an ubuntu container on arm64 architecture (confirmed). From there I am using a tool called Buildroot to create a custom linux filesystem. which after much waiting creates “rootfs.tar”

Okay now with all that non docker stuff out of the way. I copy this rootfs.tar file to my host and try to build an image to run my custom linux.

Docker file

FROM scratch
MAINTAINER peskyadmin
ADD rootfs.tar /

build command

docker buildx build -t "${DOCKER_USER}/testrtfs:latest" --platform linux/arm64 --push .

run command

docker run --privileged --entrypoint "/bin/sh" -it "$DOCKER_USER/testrtfs:latest" --platform linux/arm64

run output

WARNING: The requested image's platform (linux/arm64) does not match the detected host platform (linux/amd64) and no specific platform was requested
standard_init_linux.go:228: exec user process caused: exec format error

Using the latest version of Docker Desktop

Two observatation:
1.) an argument that follows the image name is treated as container command, and therefor will override the images CMD instruction or if an entrypoint script is present will be used as argument for it.
2.) recent docker versions (i checked 20.10.10) in fact support the --platform argument, but only if the server is multi-platform capable. The argument needs be positioned before the image.

Though. I have no idea what qualifies a server to be multi-platform capable.
Update: having QEMU installed seem to qualify the server to be multi-platform capable. In the last example of the emulation section from this blog post it seems it does not even require the --platform argument at all to run the container if QEMU is installed.