從 Compose 檔案使用 Bake 進行建置
目錄
Bake 支援 Compose 檔案格式,可解析 Compose 檔案並將每個服務轉換為一個 目標 (target)。
# 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$ 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"]
}
}
}與 HCL 格式相比,Compose 格式有一些限制
- 目前不支援指定變數或全域範圍屬性
- 不支援
inherits服務欄位,但您可以使用 YAML 錨點 (anchors) 來參考其他服務,如先前使用&build-dev的範例所示。
.env 檔案
您可以在名為 .env 的環境檔案中宣告預設環境變數。此檔案將從執行指令的當前工作目錄載入,並應用於透過 -f 傳入的 Compose 定義。
# 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"]
}
}
}注意系統環境變數的優先順序高於
.env檔案中的環境變數。
使用 x-bake 的擴充欄位
當 Compose 規格中沒有某些欄位時,您可以在您的 Compose 檔案中使用 特殊擴充 欄位 x-bake 來評估額外欄位
# compose.yaml
services:
addon:
image: ct-addon:bar
build:
context: .
dockerfile: ./Dockerfile
args:
CT_ECR: foo
CT_TAG: bar
x-bake:
tags:
- ct-addon:foo
- ct-addon:alp
platforms:
- linux/amd64
- linux/arm64
cache-from:
- user/app:cache
- type=local,src=path/to/cache
cache-to:
- type=local,dest=path/to/cache
pull: true
aws:
image: ct-fake-aws:bar
build:
dockerfile: ./aws.Dockerfile
args:
CT_ECR: foo
CT_TAG: bar
x-bake:
secret:
- id=mysecret,src=./secret
- id=mysecret2,src=./secret2
platforms: linux/arm64
output: type=docker
no-cache: true$ docker buildx bake --print
{
"group": {
"default": {
"targets": ["addon", "aws"]
}
},
"target": {
"addon": {
"context": ".",
"dockerfile": "./Dockerfile",
"args": {
"CT_ECR": "foo",
"CT_TAG": "bar"
},
"tags": ["ct-addon:foo", "ct-addon:alp"],
"cache-from": [
{
"ref": "user/app:cache",
"type": "registry"
},
{
"src": "path/to/cache",
"type": "local"
}
],
"cache-to": [
{
"dest": "path/to/cache",
"type": "local"
}
],
"platforms": ["linux/amd64", "linux/arm64"],
"pull": true
},
"aws": {
"context": ".",
"dockerfile": "./aws.Dockerfile",
"args": {
"CT_ECR": "foo",
"CT_TAG": "bar"
},
"tags": ["ct-fake-aws:bar"],
"secret": [
{
"id": "mysecret",
"src": "./secret"
},
{
"id": "mysecret2",
"src": "./secret2"
}
],
"platforms": ["linux/arm64"],
"output": [
{
"type": "docker"
}
],
"no-cache": true
}
}
}x-bake 的有效欄位完整清單
cache-fromcache-tocontextsno-cacheno-cache-filteroutputplatformspullsecretsshtags