목차

, , , , ,

Docker Bake

Docker Buildx Bake는 HCL, JSON 또는 Compose YAML 정의 파일에서 여러 image build target을 선언하고 병렬 실행하는 고수준 build 명령이다.

Summary

Usage

docker buildx bake [OPTIONS] [TARGET...]
docker bake [OPTIONS] [TARGET...]

Installation

Docker Desktop에는 Buildx와 BuildKit이 포함된다. Docker Engine과 Docker CLI를 Docker 공식 repository에서 관리하는 Linux 환경은 docker-buildx-plugin package를 설치한다.

# Debian / Ubuntu
sudo apt-get update
sudo apt-get install docker-buildx-plugin
 
# RHEL / Fedora
sudo dnf install docker-buildx-plugin
 
# 설치 확인
docker buildx version
docker buildx bake --help

Bake File Authoring

File Formats and Lookup Order

Bake 정의는 HCL, JSON, Compose YAML 형식을 지원한다. HCL은 variable, function 등 Bake 전용 기능을 온전히 사용할 수 있는 기본 작성 형식이다.

-f 또는 –file을 생략하면 현재 directory에서 다음 순서로 파일을 찾고, 발견된 파일을 순서대로 병합한다.

  1. compose.yaml
  2. compose.yml
  3. docker-compose.yml
  4. docker-compose.yaml
  5. docker-bake.json
  6. docker-bake.hcl
  7. docker-bake.override.json
  8. docker-bake.override.hcl

뒤에서 읽은 정의가 앞의 정의를 확장한다. target.tags, target.platforms, target.output, target.dockerfile, target.dockerfile-inline, target.pull, target.target, target.cache-to처럼 교체 대상인 속성은 마지막 정의가 우선한다.

HCL Building Blocks

Minimal HCL Example

variable "REGISTRY" {
  type        = string
  default     = "registry.example.com/team"
  description = "Image registry and namespace"
}
 
variable "TAG" {
  type        = string
  default     = "dev"
  description = "Image tag"
}
 
target "_common" {
  context    = "."
  dockerfile = "Dockerfile"
  pull       = true
}
 
target "app" {
  inherits  = ["_common"]
  target    = "runtime"
  tags      = [format("%s/app:%s", REGISTRY, TAG)]
  platforms = ["linux/amd64", "linux/arm64"]
}
 
target "worker" {
  inherits = ["_common"]
  target   = "worker"
  tags     = [format("%s/worker:%s", REGISTRY, TAG)]
}
 
group "default" {
  targets = ["app", "worker"]
}

Inheritance

공통 target을 먼저 정의하고 inherits로 재사용한다. 여러 parent를 지정하면 목록에서 뒤에 있는 parent의 충돌 값이 우선한다.

target "_release" {
  pull = true
  attest = [
    "type=provenance,mode=max",
    "type=sbom"
  ]
}
 
target "app-release" {
  inherits  = ["app", "_release"]
  tags      = ["registry.example.com/team/app:latest"]
  platforms = ["linux/amd64", "linux/arm64"]
}

Matrix

matrix는 하나의 target 정의에서 여러 variant를 만든다. 생성되는 각 target의 이름은 name으로 고유하게 지정한다.

target "app" {
  name = "app-${platform}"
 
  matrix = {
    platform = ["amd64", "arm64"]
  }
 
  platforms = ["linux/${platform}"]
  tags      = ["registry.example.com/team/app:${platform}"]
}

Compose YAML Build Definition

기존 compose.yaml의 service build 속성도 Bake target으로 사용할 수 있다.

services:
  app:
    image: registry.example.com/team/app:dev
    build:
      context: .
      dockerfile: Dockerfile
      target: runtime
      platforms:
        - linux/amd64
        - linux/arm64

Compose 파일과 docker-bake.hcl이 함께 있으면 둘을 병합한다. Compose의 service build 정의를 기본값으로 두고 HCL에서 tag, output, cache, attestation을 확장할 수 있다.

Examples

Inspect Before Build

# target과 설명 확인
docker buildx bake --list=targets
 
# variable, type, default, 설명 확인
docker buildx bake --list=variables
 
# 병합 후 실제 build definition 확인
docker buildx bake --print
 
# Dockerfile build check 실행
docker buildx bake --check

Build Targets

# default group build
docker buildx bake
 
# app target만 build
docker buildx bake app
 
# 여러 target 병렬 build
docker buildx bake app worker
 
# 결과를 local Docker image store로 load
docker buildx bake --load app
 
# release target을 registry로 push
docker buildx bake --push app-release

Override Values

# Bake variable override
docker buildx bake --var TAG=2026.08.17 app
 
# environment variable로 같은 이름의 variable override
TAG=2026.08.17 docker buildx bake app
 
# 모든 target의 platform 교체
docker buildx bake --set "*.platform=linux/amd64"
 
# app 계열 target에 tag 추가
docker buildx bake --set "app*.tags+=registry.example.com/team/app:stable"
 
# 특정 target의 build argument override
docker buildx bake --set app.args.BUILD_MODE=release app

Multiple Definition Files

# 공통 정의 뒤에 release override 병합
docker buildx bake \
  -f docker-bake.hcl \
  -f docker-bake.release.hcl \
  --print
 
# 병합된 release target build 및 push
docker buildx bake \
  -f docker-bake.hcl \
  -f docker-bake.release.hcl \
  --push app-release

Troubleshooting

Compatibility

Help

docker buildx bake --help

Security

Registry credential, private key, API token을 args, tags, environment variable default 또는 Bake 파일에 평문으로 넣지 않는다. BuildKit secretssh mount를 사용하고, –allow, security.insecure, network.host, filesystem wildcard 권한은 build에 필요한 최소 범위만 명시한다.

See Also

History