How to Build With Docker Buildx Bake in CI
Buildx bake reads a declarative file of targets and builds them in one command, so build config lives in the repo.
Describe targets in docker-bake.hcl (contexts, tags, platforms, args), then run docker/bake-action. CI builds everything defined there without long inline command flags.
Steps
- Write
docker-bake.hclwith one or moretargetblocks and agroup. - Set up Buildx in the workflow.
- Run
docker/bake-actionpointing at the file and a target or group.
docker-bake.hcl
docker-bake.hcl
group "default" {
targets = ["api", "web"]
}
target "api" {
context = "./services/api"
tags = ["ghcr.io/acme/api:latest"]
}
target "web" {
context = "./services/web"
tags = ["ghcr.io/acme/web:latest"]
platforms = ["linux/amd64", "linux/arm64"]
}Workflow
.github/workflows/docker.yml
- uses: docker/setup-buildx-action@v3
- uses: docker/bake-action@v5
with:
files: docker-bake.hcl
targets: default
push: trueGotchas
- A bake
grouplets you build several targets at once; pass a single target name to build just one. - HCL variables and functions keep tags and args DRY across targets.
Related guides
How to Build a Matrix of Docker Image Variants in CIBuild several Docker image variants (different base versions or flavors) in parallel in CI using a build matr…
How to Build and Push a Multi-Arch Docker Image in CIBuild a single Docker manifest covering linux/amd64 and linux/arm64 in CI with QEMU and Buildx, then push it…