I just bought my new M1 Apple Silicon Mac Mini, and after about a week of using it I ran into an issue related to different architectures and deployment of Docker containers. I can build and run images on my M1 Mac fine, but when I try and push the image built on the Apple Silicon Mac to a cloud server running Linux distro, I see the following error:
standard_init_linux.go:211: exec user process caused "exec format error"
This was troubling to say the least. So for awhile I just used my old AMD64 Mac to compile and push my containers. But this is not ideal, so how can we fix this?
You must install Rosetta 2 as some binaries are still Darwin/AMD64. To install Rosetta 2 manually from the command line, run the following command:
Buildx comes with a default builder that you can see by typing
docker buildx ls into your terminal. However, we want to make and use a builder ourselves.
Make a builder
You will need a new builder, to make one use
docker buildx create --name m1_builder. Now you should see the new builder when running
docker buildx ls.
Bootstrap the Builder
The next steps are to use the new builder then bootstrap it. To allow docker to use new builder run,
docker buildx use m1_builder and then
docker buildx inspect --bootstrap, which will inspect and bootstrap the builder instance you’re using.
Build with the builder
Now, navigate to where you want to build and push an image so you can run this pseudo-command:
docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 --tag <remote image repository> --push ./
Once your image has been built and pushed, you can inspect the manifest by running:
docker buildx imagetools inspect <remote image repository>
You should see a manifest printout with all your different architectures.
Finally, to clean up any extra disk space usage you can run
docker buildx prune --all to reduce the Docker footprint.