Docker build on Mac M1

#docker #mac
up::DevOps Commands
Also find this article on Medium

apple_new-imac-spring21_hero_04202021_Full-Bleed-Image.jpg.large_2x.jpg

A docker container is supposed to work everywhere and eliminate the need to worry about dependencies, right? See Docker Overview. I was frustrated when a Docker image built on my Mac M1 did not work on my Linux machine. Well, it turns out you need to do a little more if you have a Mac with the new M1 chip.

The M1 uses arm64 architecture instead of the amd64 architecture used on most machines. Any image you build on a Mac M1 and run on another machine will give you the following error message.

WARNING: The requested image's platform (linux/arm64/v8) does not match the detected host platform (linux/amd64/v3) and no specific platform was requested

If you run a docker ps you will find there are no containers running. If you run the following command, you will see the following error.

Docker logs 
exec /usr/local/bin/python: exec format error

To resolve this, on a Mac M1 always build images with the platform switch.

docker build \  
-t image_name:1.0 \  
--platform linux/amd64 \  
.

This will allow an image built on the Mac M1 (with arm64) to work on a Linux machine (typically with amd64). You can still run this amd64 image on a Mac M1 even though it gives you the following warning message.

WARNING: The requested image’s platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested

You can build a multi-platform image to create a docker image for all the architectures you expect to need and avoid any errors, but it is much more complicated.