使用 Bake 从 Compose 文件进行构建

Bake 支持 Compose 文件格式来解析 Compose 文件并将每个服务转换为目标

# docker-compose.yml
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": ["docker.io/username/webapp:cache"],
      "cache-to": ["docker.io/username/webapp:cache"]
    },
    "webapp-release": {
      "context": ".",
      "dockerfile": "Dockerfile.webapp",
      "tags": ["docker.io/username/webapp:latest"],
      "cache-from": ["docker.io/username/webapp:cache"],
      "cache-to": ["docker.io/username/webapp:cache"],
      "platforms": ["linux/amd64", "linux/arm64"]
    }
  }
}

与 HCL 格式相比,compose 格式有一些限制:

  • 尚不支持指定变量或全局范围属性
  • inheritsservice 字段,但您可以使用 YAML 锚点来引用其他服务,如前面的示例所示。&build-dev

.env 文件

您可以在名为 的环境文件中声明默认环境变量。此文件将从当前工作目录 其中,执行命令并将其应用于传递的 Compose 定义 跟。.env-f

# docker-compose.yml
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 的 Extension 字段

如果某些字段在 Compose 规范中不可用,您可以使用 Compose 文件中的特殊扩展名字段,用于评估额外的字段:x-bake

# docker-compose.yml
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": ["aws", "addon"]
    }
  },
  "target": {
    "addon": {
      "context": ".",
      "dockerfile": "./Dockerfile",
      "args": {
        "CT_ECR": "foo",
        "CT_TAG": "bar"
      },
      "tags": ["ct-addon:foo", "ct-addon:alp"],
      "cache-from": ["user/app:cache", "type=local,src=path/to/cache"],
      "cache-to": ["type=local,dest=path/to/cache"],
      "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-from
  • cache-to
  • contexts
  • no-cache
  • no-cache-filter
  • output
  • platforms
  • pull
  • secret
  • ssh
  • tags