Docker Cross-Platform Build
Introduction
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
Install Docker
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.
1 | $ sudo apt-get update |
Install Emulation Dependencies
To emulate the compilation and building on other platforms, we have to install QEMU.
1 | $ sudo apt-get install binfmt-support qemu-user-static |
Select Base Image
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.
1 | $ docker manifest inspect --verbose pytorch/pytorch:1.7.0-cuda11.0-cudnn8-devel |
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.
1 | $ docker manifest inspect --verbose ubuntu:20.04 |
In the latest Docker, buildx
was installed by default. We could also examine the Docker images using the following command.
1 | $ docker buildx imagetools inspect ubuntu:20.04 |
Prepare Dockerfile
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.
1 | FROM ubuntu:20.04 |
Build Docker Image
To build Docker image on other platforms, we have to create a builder first.
1 | $ docker buildx create --use --name cross-platform-build |
The new builder supports emulating the building on multiple platforms, such as amd64, arm64, and riscv64, etc.
1 | $ docker buildx inspect --bootstrap cross-platform-build |
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.
1 | $ docker login |
To run Docker image build and push the built images, please run the following command.
1 | $ docker buildx build -f opencv.Dockerfile --platform linux/amd64,linux/arm64 -t leimao/opencv:4.5.0 --push . |
1 | $ docker buildx imagetools inspect leimao/opencv:4.5.0 |
Once the Docker images has been pushed, we could run the Docker container on both the amd64 and arm64 platform.
1 | $ docker pull leimao/opencv:4.5.0 |
To test if OpenCV has been built and installed successfully, please run the following command.
1 | $ cd /tmp |
To check if OpenCV-Python has been built and installed successfully, please run the following command.
1 | $ python |
References
Docker Cross-Platform Build