Docker Community Forums

Share and learn in the Docker community.

Building on x86 (Windows) a Docker image to run on Raspberry Pi an executable also built in image

I have a C app whose executable I would like to run in a Docker container on an ARM-based Raspberry Pi.

Ideally, as part of creating the Docker image on my Windows laptop I would like to build the app from source, targetted of course for the Pi. But despite using a base image of arm32v7:latest for the image, gcc builds it for x86 (and I tried several other images).

I know there are clunky workarounds, such as pre-building the app natively on the Pi and copying the executable into the Docker image during the latter’s build, or deferring the app build until the image starts running on the Pi as a container. But it seems more efficient and elegant to pre-build it in the image if possible.

Any ideas? Is there a recommended Docker pattern/practice for this?

                                                             - o -

P.S. I just asked the same question on devops.stackexchange, but unfortunately it ended up so long I doubt if anyone will bother reading it there.

1 Like

Interesting.

I don’t know an answer but if this were my problem I would contact Jeff Geerling

This image does not need to be run manually. Instead, there is a helper script to execute build commands on source code existing on the local host filesystem. This script is bundled with the image.

To install the helper script, run one of the images with no arguments, and redirect the output to a file:

docker run --rm CROSS_COMPILER_IMAGE_NAME > ./dockcross
chmod +x ./dockcross
mv ./dockcross ~/bin/
Where CROSS_COMPILER_IMAGE_NAME is the name of the cross-compiler toolchain Docker instance, e.g. dockcross/linux-armv7.

Only 64-bit x86_64 images are provided; a 64-bit x86_64 host system is required.

Once again lewish95 the Let me Google that for you bot has committed plagiarism. (ref)

Many thanks clocks. I did think lewish95’s reply had a “missing Episode 1” feel to it, and no links. But I refrained from commenting, in the hope that further replies would elucidate it, as yours has done!

I’ll certainly try dockcross, but now I’m starting to think the “clunky” solution I mentioned of pre-building the target binaries then copying these into the image while building the latter is actually the best solution, because:

(1) The app build and image build tasks will be decoupled and can be done independently

(2) It will be easier to find and install tools to do the app build, without worrying about whether they are compatible with the base image

(3) The final image will be smaller, as it need not contain the build tools such as gcc etc. (In theory, for a one step app+image build these could be included in the image, used for the app build in it, and then removed from the image, but then that becomes even more cumbersome).

So with this procedure of doing a pre-build of the app for the final target architecture, this is no longer really a Docker problem as such.

2 Likes

You probably want to create a multi-arch image. Take a look at this link where an example is given for images that run both on amd and arm archs.

Have you tried docker buildx? I have used it to cross-compile arm64 and power9 on x86 hosts. Just make sure you don’t have your base image (ie. ubuntu:18.04) already pulled so it can pull the version for your target host.