Let’s see how to use Docker for Arm software development using the new buildx feature on Linux to create multi-architecture container images and run them. I’m using Ubuntu 18.04, but the same info applies to most any Linux distribution. Enough theory!!! Do you really want to see it in action? Under this blog post, I will showcase how I built ARM-based Docker Images for my tiny Raspberry cluster using `docker buildx’ utility which runs on my Docker Desktop for Mac. Installing Docker Desktop for Mac 184.108.40.206.
Building and deploying applications on IoT devices are sometimes slow due to the SoC on the IoT devices are usually not as fast as the CPU we normally used on our desktop. To make the building faster, we could download cross-compilation tools for certain platforms, run cross-platform compilations on our desktop targeting certain platforms, and copy the binaries to target device once the compilation is done.
For Docker container users on IoT devices, such cross-platform building solution from source code is sometimes inconvenient, since they not only have to build the applications but also create a software environment for the same platform inside Docker container.
Fortunately, Docker has created a building tool Buildx that emulates the Docker image building process on targeting platforms. This means, for example, we could build an arm64 Docker image from our local amd64 desktop, push the Docker image to Docker Hub, and download the Docker image from Docker Hub to the target arm64 device.
In this blog post, I would like to show how to use Docker Buildx to build cross-platform Docker images.
Cross-Platform Buildx Build
For completeness, I have included the protocol for Docker installation on Ubuntu on amd64 platform. The complete protocol could be found from the official Docker installation instructions.
Install Emulation Dependencies
To emulate the compilation and building on other platforms, we have to install QEMU.
Select Base Image
Docker Install Build Tools
To build a Docker image that is compatible with platforms other than our local amd64 platform, we have to select the base image carefully. If the base image was not built on the target device, the Docker build emulation will not be successful.
For an example, the PyTorch 1.7.0 image
pytorch/pytorch:1.7.0-cuda11.0-cudnn8-devel from Docker Hub was only built on the amd64 platform. So definitely we could not use this as a base image for creating a Docker image for the arm64 platform.
The Ubuntu 20.04 image from Docker Hub, however, has multiple manifests. It has builds for amd64, arm-v7, arm64-v8, etc. So we could use
ubuntu:20.04 as a base image for creating a Docker image for the aarch64 platform.
In the latest Docker,
buildx was installed by default. We could also examine the Docker images using the following command.
In this example, we would like to build an OpenCV Docker image for both the amd64 and the arm64 platform from our local amd64 computer.
Build Docker Image
To build Docker image on other platforms, we have to create a builder first.
The new builder supports emulating the building on multiple platforms, such as amd64, arm64, and riscv64, etc.
With this builder, we can specify the platform we want to build the image on. Once the build is complete, the Docker images with the amd64 and the arm64 manifests will be uploaded to Docker Hub. To make sure the image upload is successful, please make sure we have logged in Docker Hub using the following command.
Install Docker Buildkit
Install Docker Build
To run Docker image build and push the built images, please run the following command.
Once the Docker images has been pushed, we could run the Docker container on both the amd64 and arm64 platform.
To test if OpenCV has been built and installed successfully, please run the following command.
To check if OpenCV-Python has been built and installed successfully, please run the following command.