Bake에서의 상속
Bake에서의 상속 (Inheritance in Bake)
Bake 타깃은 다른 타깃의 속성을 상속할 수 있어요. inherits 속성을 쓰면 공통 구성을 한 번 정의해 두고 여러 타깃에서 재사용할 수 있답니다.
출처: 문서
본문
예를 들어 개발 환경용 Docker 이미지를 빌드하는 타깃이 있다고 해 봐요:
target "app-dev" {
args = {
GO_VERSION = "1.26"
}
tags = ["docker.io/username/myapp:dev"]
labels = {
"org.opencontainers.image.source" = "https://github.com/username/myapp"
"org.opencontainers.image.author" = "[email protected]"
}
}
이제 릴리스용 타깃을 만들 때 inherits 속성으로 app-dev를 상속하고 일부 속성만 덮어쓸 수 있어요:
target "app-release" {
inherits = ["app-dev"]
tags = ["docker.io/username/myapp:latest"]
platforms = ["linux/amd64", "linux/arm64"]
}
공용 재사용 타깃 (Common reusable targets)
공통으로 쓰일 빌드 인자를 정의해 둔 타깃을 하나 만들고, 다른 타깃들이 그것을 상속하게 할 수 있어요:
target "_common" {
args = {
GO_VERSION = "1.26"
BUILDKIT_CONTEXT_KEEP_GIT_DIR = 1
}
}
그리고 lint, docs, test, binaries 같은 타깃들이 _common을 상속해서 공유 속성을 적용해요:
target "lint" {
inherits = ["_common"]
dockerfile = "./dockerfiles/lint.Dockerfile"
output = [{ type = "cacheonly" }]
}
target "docs" {
inherits = ["_common"]
dockerfile = "./dockerfiles/docs.Dockerfile"
output = ["./docs/reference"]
}
target "test" {
inherits = ["_common"]
target = "test-output"
output = ["./test"]
}
target "binaries" {
inherits = ["_common"]
target = "binaries"
output = ["./build"]
platforms = ["local"]
}
상속받은 속성 덮어쓰기 (Overriding inherited attributes)
상속받은 타깃의 속성을 덮어쓰려면 하위 타깃에서 그 속성을 다시 지정하면 돼요. 아래 예시는 _common의 GO_VERSION = "1.26"을 "1.17"로 덮어써요:
target "app-dev" {
inherits = ["_common"]
args = {
GO_VERSION = "1.17"
}
tags = ["docker.io/username/myapp:dev"]
}
속성 덮어쓰기에 대한 자세한 내용은 Overriding configurations 페이지를 참고해요.
여러 타깃에서 상속받기 (Inherit from multiple targets)
inherits 목록에 여러 타깃을 나열해 복수 타깃에서 상속받을 수 있어요:
target "_common" {
args = {
GO_VERSION = "1.26"
BUILDKIT_CONTEXT_KEEP_GIT_DIR = 1
}
}
target "app-dev" {
inherits = ["_common"]
args = {
BUILDKIT_CONTEXT_KEEP_GIT_DIR = 0
}
tags = ["docker.io/username/myapp:dev"]
labels = {
"org.opencontainers.image.source" = "https://github.com/username/myapp"
"org.opencontainers.image.author" = "[email protected]"
}
}
target "app-release" {
inherits = ["app-dev", "_common"]
tags = ["docker.io/username/myapp:latest"]
platforms = ["linux/amd64", "linux/arm64"]
}
겹치는 속성이 있으면 inherits 목록에서 마지막에 등장하는 타깃이 우선해요.
타깃에서 단일 속성 재사용하기 (Reusing single attributes from targets)
타깃 전체를 상속하는 대신, target.foo.tags처럼 점 표기법으로 특정 타깃의 단일 속성만 재사용할 수도 있어요:
target "foo" {
dockerfile = "foo.Dockerfile"
tags = ["myapp:latest"]
}
target "bar" {
dockerfile = "bar.Dockerfile"
tags = target.foo.tags
}