Docker build with Mac M1
Minimal guide to building your image for multiple architectures
After more than a decade of using Macs, I recently purchased a Mac (mini) M1 to replace my dying laptop. I had been hesitant to get one due to potential software incompatibility, but thankfully things turned out to be fine. Mostly. I had no issues with Docker, until the build was pulled onto a live ubuntu server (see error in image above). Oops. I had assumed Docker meant “it-works-everywhere”, but I forgot about architecture.
Disclaimer here that I had recently switched to using Docker for local development, from Ampps - I barely understood a Dockerfile. And since I am a double beginner (both M1 and Docker), this is not an expert’s guide. Because I had found a lot of articles that were lengthy (and honestly I did not read), here I will only attempt a minimal answer and list the steps I took — as a log for future reference and in case anyone else needs it:
Solution Steps
Docker has an extended build, called ‘buildx’, and it should come with the Docker installation already. We will use this to build the image.
Step 1
We will need to create a “builder”. I will call it mybuilder
.
docker buildx create --name mybuilder
Step 2
Then we tell buildx to use mybuilder
.
docker buildx use mybuilder
Step 3
We can inspect mybuilder
just to be sure.
docker buildx inspect --bootstrap
We will see some printout, but specifically I checked for this:
linux/arm64
and linux/amd64
are both listed (image by author).Step 4a
Build the image (assuming you are in the directory where your Dockerfile is).
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 in to 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.
Extras
We can check the current builder by entering:
docker buildx ls
The current builder will have a *
beside it.
Et voila! C’est tout. I hope this is simple and direct enough, and hopefully I didn’t make any mistakes.