Creating Docker Images for different platforms

cloudnative

I switched back to using Apple last November (2021). In the beginning, I was pretty unclear why people in the DevOps space were talking about things that do or do not work. To me, everything was working 😂

That is until I realised that the docker images I built on my M1 Apple Silicone did not run in my Kubernetes clusters.

I was largely ignoring it until I had to update my example images. After all, they were still working on my Kubernetes clusters.

The Problem

If you build your Docker images as you would normally on an M1, the image will also only run on an M1. This might seem obvious to a lot of people. However, not thinking about it, it was not obvious to me until I ran into the problem. For your information, I wrote this blog post a long time ago and just never published it.

Thus, I started reading about creating multiple builds so that my images run both on M1 and on Intel chips.

So here is a little tutorial to build your Docker images for different platforms.

The process

This is copied more or less from the following blog: https://medium.com/geekculture/docker-build-with-mac-m1-d668c802ab96

First, you need an application for which you would like to build a container image.

In this example, we are using my react example app.

Usually, I would run a command similar to the following:

docker build -t anaisurlichs/react-example:0.0.6 .

However, the image would then be built for the machine's architecture that you are on.

The new process:

Step 1
Docker provides by default builders. With the following command, we will create a new builder:

docker buildx create --name mybuilder

Step 2

Then we tell buildx to use our new mybuilder.

docker buildx use mybuilder

Step 3

We can inspect mybuilder just to be sure.

❯ docker buildx inspect --bootstrap
Name:   mybuilder
Driver: docker-container

Nodes:
Name:      mybuilder0
Endpoint:  unix:///var/run/docker.sock
Status:    running
Platforms: linux/arm64, linux/amd64, linux/amd64/v2, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/mips64le, linux/mips64, linux/arm/v7, linux/arm/v6

This will tell us the different platforms that we can build our docker image for.

Step 4a

Lastly, we can build the image (assuming you are in the directory with your Dockerfile):

docker buildx build --tag [image-tag] -o type=image --platform=linux/arm64,linux/amd64 .

Step 4b

We can also build and push to the Docker Hub directly (assuming already logged into Docker):

docker buildx build --push --tag [docker-hubid/image-tag] --platform=linux/arm64,linux/amd64 .

To quickly double-check, we can log into Docker Hub. Under Tags & Scans, we can see the push attempt and under the OS column, we can see all the architectures listed on mouseover.

Credits

Again, the commands and some of the instructions were taken from a blog I found on Medium.

Original blog post: https://medium.com/geekculture/docker-build-with-mac-m1-d668c802ab96

Go and follow Lois.