Building multi arch container images with GitLab pipelines.

Oleh Adam Dubnytskyy
Platform engineering space
2 min readJun 12, 2023

Goal is to build multi arch images for amd64 and arm64 platforms by leveraging GitLab CI.

arm64 platform specific image can be levereged by AWS Graviton, a 64-bit ARM-based CPU.

Here I’ll elluminate the steps and relevant commands:

  1. Install buildx cli plugin into gitlab runner docker executor image
#Install docker buildx cli plugin
RUN mkdir -p $HOME/.docker/cli-plugins && \
wget -O $HOME/.docker/cli-plugins/docker-buildx https://github.com/docker/buildx/releases/tag/v0.10.5/buildx-v0.10.5.linux-amd64 && \
chmod +x $HOME/.docker/cli-plugins/docker-buildxd

2. GitLab pipeline bits

variables:
REGISTRY_IMAGE: "ecr.region.amazonaws.com/your-repository"
PLATFORM_ARCHITECTURE: "amd64"
...
release:docker-image:
image: your-choice-container-registry/repository/docker-executor-image:latest # Container image build in step 1
stage: release
script:
- ./gradlew release --project-prop tag=${CI_COMMIT_TAG} --project-prop image=${REGISTRY_IMAGE} --project-prop arch=${PLATFORM_ARCHITECTURE}

3. Gradle task bits

  • Gradle build tool is matter of choice. Gradle wrapper takes care of prerequisites like builder creation and management as well as the actual container image building process.
  • release task consists of two parts: creation of builder instance and the actual platform specific container image build process
task release() {
doLast {
exec {
workingDir './'
executable 'docker'
args 'buildx', 'create', '--name', 'multiarch-builder', '--driver', 'docker-container'
}
exec {
workingDir './'
executable 'docker'
args 'buildx', 'use', 'multiarch-builder'
}
exec {
workingDir './'
executable 'docker'
args 'buildx', 'inspect', '--bootstrap'
}
exec {
workingDir './'
executable 'docker'
args = ['buildx', 'build', '-t', "$image/$arch:$tag", '-t', "$image/$arch:latest", '--platform', "linux/$arch", '--push', '--progress', 'plain', '.']
}
}
}

For your convenience here are the same commands listed above but in a generic form.

  • Create builder instance
docker buildx create --name multiarch-builder --driver docker-container
docker buildx use multiarch-builder
docker buildx inspect --bootstrap
  • Build the actual platform specific container image
docker buildx build -t $image/$arch:$tag -t $image/$arch:latest --platform linux/$arch --push --progress plain .

--

--