Docker build Multi-Platform Images
Docker buildx
allows you to build multi-platform images to support different types of architectures, in particular, Linux amd64
and arm
variants.
The Docker manifesto says “build once, deploy anywhere”. That’s why, with the growing popularity of ARM architectures, it’s becoming more and more important to allow users to easily build Docker images that can automatically support multiple platforms.
Another important factor to consider is how Cloud Computing is changing recently. For example, AWS released the new A1 instances, that are based on ARM processors and are very performant and extremely cost-efficient.
That’s said, let’s see below how to build Docker images to run them on multiple platforms and architectures:
Build multi-architecture Docker images
To build a Docker multi-platform image, first, we need to create a new builder which gives access to the multi-architecture build feature:
docker buildx create --name mybuilder
now set the new builder as default:
docker buildx use mybuilder
finally, to check everything is correct, you can run inspect:
docker buildx inspect --bootstrap
If everything is correct, the output will look similar to this:
Finally, we use buildx
to build a unique Docker image that will support all the different architectures linux/amd64
, linux/arm64
and linux/arm/v7
at the same time:
docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t username/demo:latest --push .
Also, take note that --push
will automatically push the image to the registry. If you don’t want to push the image, you can just remove the --push
parameter.
Push multi-architecture image to Elastic Container Registry (ECR)
If you use ECR (Elastic Container Registry) instead of Docker Hub to store images, keep reading. Indeed, ECR, the AWS Container Registry service, has recently added support to store multi-platform Docker images!
Firstly, go to your ECR repository. Click on the “View Push Commands” button located in the repository dashboard and copy your image URL. If you are not finding the repository URL, search for something that looks like this: {ACCOUNT_ID}.dkr.ecr.{REGION}.amazonaws.com/{REPOSITORY_NAME}:latest
. Note that we are also copying the :latest
tag!
At this point, to build and push the multi-platform Docker image to ECR, we simply need to run:
docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t {ACCOUNT_ID}.dkr.ecr.{REGION}.amazonaws.com/{REPOSITORY_NAME}:latest --push .