Can I build docker image containing ARM executable when running docker build on non-ARM architecture? I have built on an x86-based Windows laptop a Docker image to be run as a container on an ARM-based architecture (Raspberry Pi 4).
Due to ending up with an x86 executable when this is built in the Dockerfile, as much as possible, including the library and app build, is deferred until the container actually runs on the Pi.
However, this seems absurd in that the build must be done each time the container runs (which it may on many Pis). No doubt it also needlessly increases the container's size (which is less important, but still annoyingly sub-optimal)
So in summary, I wonder if there is a way to run gcc in the Dockerfile and indicate that the target architecture is ARM and not the laptop on which the docker build is running.
My Dockerfile currently comprises :
FROM arm32v7/ubuntu:latest

# Defined only while image is being built
ARG DEBIAN_FRONTEND=noninteractive

# Set in container
ENV TZ=Europe/London

ADD ./src /app
WORKDIR /app

# Development only :
#
#   vim
#
RUN apt-get -y update \
 &amp;&amp; apt-get -y upgrade \
 &amp;&amp; apt-get -t install apt-utils \
 &amp;&amp; apt-get -y install tzdata \
 &amp;&amp; apt-get -y install dh-autoreconf \
 &amp;&amp; apt-get -y install libcurl4-gnutls-dev \
 &amp;&amp; apt-get -y install libexpat1-dev \
 &amp;&amp; apt-get -y install make \
 &amp;&amp; apt-get -y install gettext \
 &amp;&amp; apt-get -y install libz-dev \
 &amp;&amp; apt-get -y install libssl-dev \
 &amp;&amp; apt-get -y install libghc-zlib-dev \
 &amp;&amp; apt-get -y install git \
 &amp;&amp; apt-get -y install build-essential \
 &amp;&amp; apt-get -y install vim \
 &amp;&amp; git clone https://github.com/eclipse/paho.mqtt.c.git

CMD [&quot;/bin/bash&quot;, &quot;myapp.sh&quot;]

and myapp.sh comprises :
#!/bin/bash

# Build Paho MQTT library
#
cd /app/paho.mqtt.c
make

# Build the experiment app
#
cd /app

gcc -L/experiment/paho.mqtt.c/build/output -o myapp -lpaho-mqtt3a main.c

./myapp

I would have thought the first line &quot;FROM arm32v7/ubuntu:latest&quot; in the Dockerfile would tell gcc that the target architecture was ARM, but evidently not!
P.S. I know I could manually build the app outside Docker on a Pi, and simply copy the ARM executable into the Docker image. But I would rather the the app build is included as part of the image build if possible.
I've not tried this myself, but it seems that you can use docker buildx to build for multiple architectures: https://www.docker.com/blog/multi-arch-images/.