Contents

Build x86 Docker images on Mac M1

If you’re a Mac user who has been using Docker Desktop for your development environment, you might be interested in exploring an alternative.

One possible option is Colima, which is an open source tool for building and managing container-based development environments. In this article, we’ll take a look at how to use Colima as a replacement for Docker Desktop on your Mac.

Getting Started with Colima

  • Install packages

    1
    
    brew install colima docker docker-compose docker-credential-helper
    
  • Create x86 and aarch profiles

    1
    2
    3
    4
    5
    
    colima start --profile arm --cpu 4 --memory 6 --arch aarch64 --vm-type=vz #--vz-rosetta #--kubernetes
    colima start --profile amd --cpu 4 --memory 6 --arch x86_64 --vm-type=vz #--kubernetes
    colima list
    docker context use colima-amd
    docker context ls
    

In case of Fatal glibc error: CPU does not support x86-64-v2. That ‘max’ instructs QEMU to give the emulated cpu the same capabilities of the host machine cpu. And Silicon processors supports x86_64_v2.

1
  colima start --profile amd --cpu 4 --memory 6 --arch x86_64 --vm-type=vz --cpu-type max

Build multi-arch Docker images

About docker buildx

Docker Buildx is a Docker CLI plugin that extends the functionality of Docker by providing a more powerful and flexible way to build and distribute Docker images.

Some of the benefits of Docker Buildx:

  • Improved Build Efficiency: Docker Buildx can build several images in parallel, which can help increase your build efficiency.

  • Multi-Architecture Support: Docker Buildx can build images for multiple architectures like x86, ARM, etc., which can help you target more devices and platforms.

  • Flexible Build Context: Buildx offers a more flexible build context. Developers can choose to include only the required files in the build context, which makes the builds faster and lighter.

  • Better Security: Buildx allows images to be signed and verified for authenticity, which can help in ensuring the security of your software.

  • BuildKit Integration: Docker Buildx is built on top of BuildKit, which is a modern, faster, and more secure builder toolkit that can improve your builds’ speed and quality.

Configure Colima + Docker BuildKit

  • prepare buildx runners

    1
    2
    3
    4
    5
    6
    7
    8
    
    amd_profile=amd-buildx
    arm_profile=arm-buildx
    colima start --profile "$amd_profile" --arch x86_64 --cpu 2 --memory 4
    colima start --profile "$arm_profile" --arch aarch64 --cpu 2 --memory 4
    colima list
    docker buildx create --use --name custom colima-"$amd_profile"
    docker buildx create --append --name custom colima-"$arm_profile"
    docker buildx ls
    
  • Prepare Dockerfile (an example)

    1
    2
    3
    
    FROM alpine
    
    RUN apk add --no-cache curl
    
  • build and push images based on above Dockerfile

    1
    
    docker buildx build --file Dockerfile --platform linux/amd64,linux/arm64/v8 --no-cache --tag "myimage" --push --force-rm .
    
  • Clean buildx and colima profiles

    1
    2
    3
    
    docker buildx rm custom
    colima delete -f "$amd_profile"
    colima delete -f "$arm_profile"