Cross Platform Docker Builds
Given the recent prominence of ARM64, Docker images lately need to be built for two flavours and this may even expand more in future RISC-V. However tooling behind this has been really complicated until recently, where people needed to configure and use specific builders, which was pretty complicated stuff. However recently this has been simplified greatly, specially if you're on macOS.
If you're on the latest Docker CE
version 20.10.24 build 297e128 then I'd recommend you to use
docker compose to configure your builds similar to this:
version: "3" services: service_name: container_name: container_name platform: linux/amd64 image: sample-service:latest build: .
The important bit in the above is
platform: linux/amd64. The above will work assuming you've a Dockerfile in the current directory. If we build and inspect the docker image using
docker image inspect sample-service:latest then in the output we'll see the following:
"Architecture": "amd64", "Os": "linux",
This basically means that the image built is indeed compatible with AMD64. And it does indeed work in reverse where if you're on macOS running on Intel processors, you can compile the ARM64 images as well. This is because Docker Desktop transparently defaults to virtualization to run linux, in which it runs containers. Thus I guess, it simply starts VM with different architecture. It would be interesting to see what happens when we start containers of 2 different architectures within same networking namespace, and how Docker handles it.
This however fails to work on Linux, as Linux handles the container natively and thus fails to build when the same directive is used. Here is the sample log below:
#0 0.527 exec /bin/sh: exec format error
Hope this saves you some time compared to whatever trick you used till now.