DEV Community

Maxim Tacu
Maxim Tacu

Posted on • Updated on

Build cross-platform container images within MacOS

Photo by Philippe Oursel on Unsplash
As an experienced Site Reliability Engineer (SRE), I understand the importance of working with containerization to simplify deployment and management of applications. One tool that has proven invaluable for this task is Docker. In this blog post, I will introduce you to the Docker buildx command and demonstrate how to use it for building cross-platform container images on MacOS using colima.

What is Docker Buildx?

Docker Buildx is a CLI plugin that extends the Docker command with new features, such as building multi-platform images, using build cache efficiently, and automatic pushing of images to the Docker registry. With Buildx, you can build images for multiple platforms simultaneously, making it an ideal choice for creating cross-platform container images.

Setting Up Colima for MacOS

Colima is a Docker-compatible runtime for MacOS that allows you to use Docker natively on your Mac without requiring a virtual machine. Colima is lightweight and easy to install, making it an excellent choice for MacOS users who want to use Docker.

To install Colima, follow these steps:

  1. Install Homebrew if you haven't already:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Enter fullscreen mode Exit fullscreen mode
  1. Install Colima using Homebrew:
brew install colima
Enter fullscreen mode Exit fullscreen mode
  1. Start Colima: colima start
  2. Verify that Colima is running: colima status

Using Docker Buildx with Colima

Now that we have Colima set up, let's see how to use Docker Buildx for building cross-platform container images.

  1. Install Docker Buildx
ARCH=amd64 # change to 'arm64' for M1
VERSION=v0.10.4
curl -LO https://github.com/docker/buildx/releases/download/${VERSION}/buildx-${VERSION}.darwin-${ARCH}
mkdir -p ~/.docker/cli-plugins
mv buildx-${VERSION}.darwin-${ARCH} ~/.docker/cli-plugins/docker-buildx
chmod +x ~/.docker/cli-plugins/docker-buildx
docker buildx version # verify installation
Enter fullscreen mode Exit fullscreen mode
  1. Create a new Buildx builder instance with the name "multiplatform-builder": docker buildx create --name multiplatform-builder
  2. Use the new builder instance by running: docker buildx use multiplatform-builder
  3. Verify that the builder instance is configured for multi-platform builds: docker buildx inspect --bootstrap
  4. Now, let's build a simple example Docker image for multiple platforms (e.g., linux/amd64 and linux/arm64):
docker buildx build --platform linux/amd64,linux/arm64 -t your-username/multiplatform-image:latest . --push
Enter fullscreen mode Exit fullscreen mode

Replace "your-username" with your Docker Hub username. The --push flag is used to push the image to Docker Hub once the build is complete.

Verify the multi-platform image

After the build completes, you can verify the multi-platform image on Docker Hub by visiting your image repository: https://hub.docker.com/r/your-username/multiplatform-image/tags

You should see the "latest" tag with both "linux/amd64" and "linux/arm64" platforms listed.

Conclusion

We explored how to use Docker Buildx for building cross-platform container images on MacOS with the help of Colima. Buildx simplifies the process of creating multi-platform images, which can be crucial for deploying applications on various devices and architectures. By leveraging Colima, MacOS users can now take advantage of these capabilities natively on their systems.

Top comments (0)