Info

Docker bake allows us to efficiently build multiple images at the same time.

Passing Variables

Given a variable:

variable "ENV" {
  default = "staging"
}

You can change the value of this variable like:

ENV=production docker bake

Groups

A group of target that can be targeted instead of individual targets one by one:

group "default" { targets = ["frontend", "backend"] }

While the default group will be called if nothing is passed, you can specify a target with:

docker bake TARGET

Functions

Example Semantic Versioning versions:

function "get_major_version" {
  params = [version]
  # Wrapping the pattern in () makes it return a list
  result = regex("^([0-9]+)", version)[0]
}
 
function "get_minor_version" {
  params = [version]
  # Wrapping the pattern in () makes it return a list
  result = regex("^([0-9]+\\.[0-9]+)", version)[0]
}

Example Docker Bake

variable "ENV" {
  default = "staging"
}
 
target "myapp" {
  context = "."
  dockerfile = "Dockerfile"
  tags = ["myapp:latest"]
  args = {
    ENV = ENV
  }
  secret = [
    "type=env,id=TOKEN"
  ] 
  no-cache = true
  platforms = ["linux/amd64", "linux/arm64"]
}