How to use Docker image for multi-CPU architecture

abstract business code coder
Reading Time: 3 minutes

Have you come across a situation where you were unable to run a docker container because it did not support your CPU architecture? If yes, then this is the right blog for you to follow and to allow your container to run as expected. I mostly use amd based architectures as my host machines to host my containerized applications, but I recently shifted few of my services to EC2 instances with Graviton processors, which are built on Nitro systems.These processors are based on ARM based architecture which have many advantages in terms of storage, information accessibility and power usage, making it cost effective for end users.

We know that docker images can support different architectures, which makes it a powerful tool. When running an image with multi-architecture support, docker automatically selects the image variant that matches your OS and architecture. Most of the docker official images have support for multiple architectures such as amd64, arm32v5, arm32v6, arm32v7, arm64v8, i386, ppc64le, and s390x.

Support for ARM:

Docker is continuously improving its functionality in terms of container creation and management in arm based devices. We can use standard docker tooling to build, start and push the docker images. In this blog, I have used an arm based Ubuntu:20.04 instance from AWS to create a docker image with arm architecture.

Buildx:

Lets first start by installing docker in our server

I have followed this official documentation from Docker for its installation. I am pasting the same commands below for quick references:

sudo apt-get update

sudo apt-get remove docker docker-engine docker.io containerd runc

sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli containerd.io

Post installation command:

sudo usermod -aG docker $USER

Now we will install buildx for Docker:

You can refer to this for latest releases of docker buildx

wget https://github.com/docker/buildx/releases/download/v0.8.2/buildx-v0.8.2.linux-arm64

mkdir -p ~/.docker/cli-plugins
mv buildx-v0.8.2.linux-arm64 ~/.docker/cli-plugins/docker-buildx
chmod +x ~/.docker/cli-plugins/docker-buildx
docker buildx install
docker buildx ls

This will display the platforms which is supported by docker while building:

Lets build a docker image that supports the arm64 architecture:

Write a basic Dockerfile as:

FROM ubuntu:20.04
RUN apt-get update && apt-get install -y curl

Build this Docker image

docker buildx build --platform linux/arm64 -t ubuntu:arm .

 

On inspecting the image, you will notice the architecture of the image

The OS is Linux and Arhictecture is of arm64 type which is what we needed. You can build a container out of that image in the servers which only support arm based architectures. To know which architecture is supported by your machine, type uname -m .

Hey, readers! Thank you for sticking up till the end. If you have any questions/feedbacks regarding this blog, I am reachable at vidushi.bansal@knoldus.com. You can find more of my blogs here.

References:

https://docs.docker.com/buildx/working-with-buildx/

Written by 

Vidushi Bansal is a Software Consultant [Devops] at Knoldus Inc. She is passionate about learning and exploring new technologies.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading