[DroneCI] How to Build Multiarch Docker Image for Private Registry

Few months ago, I wrote a post about DroneCI: How to configure pipeline to build and push docker image. It worked well for multiple personal projects of mine. However, recently I am working on a new project which I want to deploy the image in the VPS instances with different platforms and architecture.

1. Solution

Thanks to the Drcone CI plugin drone-docker-buildx, the task becomes a lot of easier. Here you go.

 1---
 2kind: pipeline
 3type: docker
 4name: build-multiarch-images
 5
 6platform:
 7  os: linux
 8  arch: amd64
 9
10steps:
11- name: docker
12  image: thegeeklab/drone-docker-buildx
13  privileged: true
14  settings:
15    registry: private.registry.example.com
16    username: 
17      from_secret: registry_username
18    password:
19      from_secret: registry_pwd
20    repo: repo.example.com/project
21    tags: 
22      - latest
23      - 0.0.3
24    platforms: 
25      - linux/arm64
26      - linux/amd64
27
28trigger:
29  branch:
30  - main
31  event:
32    exclude:
33    - pull_request
34    

What the above Drone CI pipeline does is:

  1. Build docker images for linux/amd64 and linux/arm64
  2. Push the built images to the self-host docker registry private.registry.example.com
  3. Trigger the CI build event only when the Pull Request is merged to the main branch

2. Explanation

There are some highlight lines:

  1. Don’t get confused by two keys platform. The first one is the platform where the docker buildx will be executed. In the other words, it is where your Drone runner is installed.
 1---
 2kind: pipeline
 3type: docker
 4name: build-multiarch-images
 5
 6platform:
 7  os: linux
 8  arch: amd64
 9
10...
  1. The Drone plugin drone-docker-buildx requires a privileged permission.
 1---
 2...
 3
 4steps:
 5- name: docker
 6  image: thegeeklab/drone-docker-buildx
 7  privileged: true
 8  settings:
 9
10...
  1. If the registry repository is not public, you need to configure the authentication. The authentication environment variables can be configured in the repository setting of the Drone CI Server
 1---
 2...
 3
 4steps:
 5- name: docker
 6  image: thegeeklab/drone-docker-buildx
 7  privileged: true
 8  settings:
 9    registry: private.registry.example.com
10    username: 
11      from_secret: registry_username
12    password:
13      from_secret: registry_pwd
14    repo: repo.example.com/project
15    tags: 
16      - latest
17      - 0.0.3
18...

drone-ci-secret

  1. Don’t forget to specify the registry repository where the built image was pushed to
 1---
 2...
 3
 4steps:
 5- name: docker
 6  image: thegeeklab/drone-docker-buildx
 7  privileged: true
 8  settings:
 9    registry: private.registry.example.com
10    username: 
11      from_secret: registry_username
12    password:
13      from_secret: registry_pwd
14    repo: repo.example.com/project
15    tags: 
16      - latest
17      - 0.0.3
18...

3. Conclusion

This post basically shows how to use docker buildx in Drone CI. It is easy and convenient, especially for users who do not have the resources with different platforms and architecture. But as a tradeoff, the CI build takes longer time.

Before the docker buildx is introduced, you can run multiple Drone runners in the different platforms and configure pipelines for each platform. That method is much faster.

Thus, there is no good or bad. Just choose the solution that suits your case best.


If this post helped you to solve a problem or provided you with new insights, please upvote it and share your experience in the comments below. Your comments can help others who may be facing similar challenges. Thank you!
Buy Me A Coffee
DigitalOcean Referral Badge
Sign up to get $200, 60-day account credit !