Compose 파일로 Bake 빌드하기

Compose 파일로 Bake 빌드하기 (Building with Bake from a Compose file)

Bake는 Compose 파일 형식을 지원해서 Compose 파일을 파싱하고 각 서비스를 빌드 타깃으로 변환해요. 이미 Compose로 서비스를 정의해 두고 있다면 별도의 Bake 파일 없이도 Bake로 그대로 빌드할 수 있답니다.

출처: 문서

본문

다음은 Compose 파일을 Bake로 빌드하는 예시예요:

# compose.yaml
services:
  webapp-dev:
    build: &build-dev
      dockerfile: Dockerfile.webapp
      tags:
        - docker.io/username/webapp:latest
      cache_from:
        - docker.io/username/webapp:cache
      cache_to:
        - docker.io/username/webapp:cache

  webapp-release:
    build:
      <<: *build-dev
      x-bake:
        platforms:
          - linux/amd64
          - linux/arm64

  db:
    image: docker.io/username/db
    build:
      dockerfile: Dockerfile.db

--print 플래그로 Bake가 이 Compose 파일을 어떻게 해석하는지 미리 확인할 수 있어요:

$ docker buildx bake --print
{
  "group": {
    "default": {
      "targets": ["db", "webapp-dev", "webapp-release"]
    }
  },
  "target": {
    "db": {
      "context": ".",
      "dockerfile": "Dockerfile.db",
      "tags": ["docker.io/username/db"]
    },
    "webapp-dev": {
      "context": ".",
      "dockerfile": "Dockerfile.webapp",
      "tags": ["docker.io/username/webapp:latest"],
      "cache-from": [
        {
          "ref": "docker.io/username/webapp:cache",
          "type": "registry"
        }
      ],
      "cache-to": [
        {
          "ref": "docker.io/username/webapp:cache",
          "type": "registry"
        }
      ]
    },
    "webapp-release": {
      "context": ".",
      "dockerfile": "Dockerfile.webapp",
      "tags": ["docker.io/username/webapp:latest"],
      "cache-from": [
        {
          "ref": "docker.io/username/webapp:cache",
          "type": "registry"
        }
      ],
      "cache-to": [
        {
          "ref": "docker.io/username/webapp:cache",
          "type": "registry"
        }
      ],
      "platforms": ["linux/amd64", "linux/arm64"]
    }
  }
}

Compose 형식의 제한사항

Compose 형식은 HCL 형식에 비해 몇 가지 제한이 있어요:

  • 변수나 전역(global) 스코프 속성을 지정하는 것은 지원되지 않아요.
  • (일부 속성은 HCL에서만 지원돼요.)

환경 변수 보간 (Interpolation)

.env 파일의 값을 활용해 TAG 같은 변수를 보간할 수 있어요:

# compose.yaml
services:
  webapp:
    image: docker.io/username/webapp:${TAG:-v1.0.0}
    build:
      dockerfile: Dockerfile
# .env
TAG=v1.1.0
$ docker buildx bake --print
{
  "group": {
    "default": {
      "targets": ["webapp"]
    }
  },
  "target": {
    "webapp": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "tags": ["docker.io/username/webapp:v1.1.0"]
    }
  }
}

x-bake 확장 필드

Compose 파일의 x-bake 필드를 사용하면 HCL에서 지원되는 추가 필드를 평가할 수 있어요:

# compose.yaml
services:
  addon:
    image: ct-addon:bar
    build:
      context: .
      dockerfile: ./Dockerfile
      args:
        CT_ECR: foo
        CT_TAG: bar
      x-bake:
        # ... 추가 속성

더 알아보기 (Learn more)