Docker image build on Mac M1 not running on Ubuntu server. How to fix it?

Docker image build on Mac M1 not running on Ubuntu server. How to fix it?

Introduction

I have been using Ubuntu for a long time on Intel Core i5 system. The daily routine was to pull the code from the Git repositories, build docker images, push it to Dockerhub and run docker containers with those images in the production Ubuntu server.

A few weeks ago one member joined our DevOps team, he was using a Macbook Pro M1. I installed the required tools in his system and he started working on projects as usual.

He built the first docker image and pushed it to Dockerhub. I deployed that image in the production server, but it failed to create a healthy container. It was going in a restart state.

The Problem

After a few debugging the issue, found out that the docker image was built upon the ARM architecture as Macbook Pro M1 is using ARM architecture.

As we know docker containers use the underlying operating system kernels, resources and drivers. So docker image is built on ARM architecture trying to run on AMD architecture which not going to work.

The Solution

The solution is simple, We have to tell docker that build the image for AMD architecture. That means this image will only be used on AMD architecture platforms.

Docker has an extended build, called ‘buildx’. It’s a CLI plugin meant for building for multiple platforms.

We will use this to build the image with the following command

docker buildx build --platform linux/amd64 \
    -t <docker-image-name> \
    --no-cache
  • buildx build: The docker buildx build command supports features available for docker build, including features such as output configuration, inline build caching, and specifying target platform.

  • — platform linux/amd64: Here we’re specifying to docker buildx the target platform we’d like to build the docker image for.

  • -t: It’s an option to tag our image and you already know it.

  • — no-cache: this option tells docker to get rid of any cached data about this image.

After running this command, Docker builds an image that can be used on the Ubuntu server.

Conclusion

Even if team members are using different OS architectures, we could build and ship the project to the server with the great tool Dockcer.