DEV Community

Lakhan Samani
Lakhan Samani

Posted on

Create docker image on Apple Silicon M1 Mac

My biggest pain while moving to Apple Silicon M1 chip mac book was to create a docker build that would work on all the machines. It took some time for me to figure out how I can create docker images and share on docker hub that would work across different platforms.

By default Docker on M1 macbook would create linux/arm64 images, which would work only on the machines that are using ARM architecture. But intel based machines uses AMD architecture. As a result docker images built on m1 macbook might not work on intel based machines.

It might not even work on gcloud / aws k8s clusters 😄

But the good part is that m1 chip macbook supports linux/amd64 images, so we might only need to build docker images using linux/amd64.

Let me demonstrate an example here:

Consider you have a Golang Application and you need Docker image for that

So your Dockerfile might look like

FROM golang:1.16-alpine as builder
WORKDIR /app
COPY . .

RUN go build && \
    chmod 777 docker-demo

FROM alpine:latest
WORKDIR /root/
COPY --from=builder /app/docker-demo .
CMD [ "./docker-demo" ]
Enter fullscreen mode Exit fullscreen mode

Note this is just an example you can have any Dockerfile😄

Now the command to build should add --platform flag with the correct platform that you want to build docker image for.

Example

docker build --platform linux/amd64 -t lakhansamani/docker-demo .
Enter fullscreen mode Exit fullscreen mode

Thats all 😅
Now you can publish your docker image on docker hub and share with people.

You can find demo code on this github repository

Discussion (11)

Collapse
pavanbelagatti profile image
Pavan Belagatti

Man, you saved so much time. Thanks

Collapse
limbuu profile image
Manshi Limbu

I almost wasted 2-3 hours, thanks for saving me :D

Collapse
mushfique profile image
Mushi123

what about running images, on mac m1, that were built on linux/amd64? Specifically, I set up Github Actions so my docker builds are happening on the Github server, which I am guessing are doing linux/amd64 builds. When I pull the image and try to run it, it runs into this error:
The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested

Any suggestions?

Collapse
lakhansamani profile image
Lakhan Samani Author

Haven't seen any such issues until your docker image uses some native api's
If its public image can you share Docker file + image name. I can take look into it.

Collapse
brooks_dubois_b5a11ed783d profile image
Brooks DuBois

It's happening with Micronaut, any ideas are appreciated.

try just getting micronaut and building a simple web app to serve you an index.html, then when you try and do dockerBuild from gradle you'll see the v8 issue.

Collapse
anggutie profile image
Angel Gutierrez

I can confirm, any image created in an M1 Mac without the —platform flag will not work in a GKE cluster…

That’s the reason why I found this post, lol.

Collapse
santhoshshivanna profile image
santhoshshivanna

I can confirm, any image created in an M1 Mac without the platform flag will not work in a ECS cluster.

That's the reason why I found this post, lol. copied from previous comment :D

Collapse
kevinlien profile image
Kevin Lien

I'm here thanks to GKE. :) Appreciate the short yet lifesaving post!

Collapse
tutorialwork profile image
Tutorialwork

For me it tooks around 5 minutes on a M1 Mac Mini to build a amd64 Docker image. Does anybody has similar experience or is the problem my Dockerfile?

Collapse
barquito profile image
Santiago Barchetta

Dude! This was so helpfull! 2 days losts trying to solve this issue and the solution was so simple!

I can confirm, this solved me issues building and deploying in GCP.

Collapse
lakhansamani profile image
Lakhan Samani Author

Thanks :-)