注释

注释提供图像的描述性元数据。使用注释记录任意信息并将其附加到您的图像中,这有助于消费者和工具了解图像的来源、内容以及如何使用图像。

注释类似于标签,并且在某种意义上与 标签重叠。两者都有相同的目的:将元数据附加到资源。作为一般原则,您可以将注释和标签之间的区别视为如下:

  • 注释描述 OCI 图像组件,例如 清单索引描述符。
  • 标签描述 Docker 资源,例如镜像、容器、网络和卷。

OCI 图像 规范定义了注释的格式以及一组预定义的注释键。遵守指定的标准可确保通过 Docker Scout 等工具自动且一致地显示有关图像的元数据。

注释不要与证明混淆 :

  • 证明包含有关图像如何构建及其包含内容的信息。证明作为单独的清单附加在图像索引上。开放容器倡议并未对证明进行标准化。
  • 注释包含有关图像的任意元数据。注释作为标签附加到图像 配置,或者作为属性附加到图像索引或清单。

添加注释

您可以在构建时或创建图像清单或索引时向图像添加注释。

笔记

Docker Engine 镜像存储不支持加载带注释的镜像。要使用注释进行构建,请确保使用--pushCLI 标志或 注册表导出器将映像直接推送到注册表。

要在命令行上指定注释,请使用命令--annotation的标志 docker build

$ docker build --push --annotation "foo=bar" .

如果您使用 Bake,则可以使用该annotations 属性为给定目标指定注释:

target "default" {
  output = ["type=registry"]
  annotations = ["foo=bar"]
}

有关如何向使用 GitHub Actions 构建的图像添加注释的示例,请参阅 使用 GitHub Actions 添加图像注释

您还可以向使用创建的图像添加注释docker buildx imagetools create。此命令仅支持向索引或清单描述符添加注释,请参阅 CLI 参考

检查注释

要查看图像索引上的注释,请使用以下docker buildx imagetools inspect命令。这会向您显示索引和索引包含的描述符(对清单的引用)的任何注释。以下示例显示了org.opencontainers.image.documentation描述符上的注释和 org.opencontainers.image.authors索引上的注释。

$ docker buildx imagetools inspect <IMAGE> --raw
{
  "schemaVersion": 2,
  "mediaType": "application/vnd.oci.image.index.v1+json",
  "manifests": [
    {
      "mediaType": "application/vnd.oci.image.manifest.v1+json",
      "digest": "sha256:d20246ef744b1d05a1dd69d0b3fa907db007c07f79fe3e68c17223439be9fefb",
      "size": 911,
      "annotations": {
        "org.opencontainers.image.documentation": "https://foo.example/docs",
      },
      "platform": {
        "architecture": "amd64",
        "os": "linux"
      }
    },
  ],
  "annotations": {
    "org.opencontainers.image.authors": "dvdksn"
  }
}

要检查清单上的注释,请使用命令docker buildx imagetools inspect并指定<IMAGE>@<DIGEST>,其中<DIGEST>是清单的摘要:

$ docker buildx imagetools inspect <IMAGE>@sha256:d20246ef744b1d05a1dd69d0b3fa907db007c07f79fe3e68c17223439be9fefb --raw
{
  "schemaVersion": 2,
  "mediaType": "application/vnd.oci.image.manifest.v1+json",
  "config": {
    "mediaType": "application/vnd.oci.image.config.v1+json",
    "digest": "sha256:4368b6959a78b412efa083c5506c4887e251f1484ccc9f0af5c406d8f76ece1d",
    "size": 850
  },
  "layers": [
    {
      "mediaType": "application/vnd.oci.image.layer.v1.tar+gzip",
      "digest": "sha256:2c03dbb20264f09924f9eab176da44e5421e74a78b09531d3c63448a7baa7c59",
      "size": 3333033
    },
    {
      "mediaType": "application/vnd.oci.image.layer.v1.tar+gzip",
      "digest": "sha256:4923ad480d60a548e9b334ca492fa547a3ce8879676685b6718b085de5aaf142",
      "size": 61887305
    }
  ],
  "annotations": {
    "index,manifest:org.opencontainers.image.vendor": "foocorp",
    "org.opencontainers.image.source": "https://git.example/foo.git",
  }
}

指定注释级别

默认情况下,注释会添加到图像清单中。您可以通过在注释字符串前添加特殊类型声明来指定将清单附加到哪个级别:

  • manifest: 注释清单。
  • index: 注释根索引。
  • manifest-descriptor:注释索引中的清单描述符。
  • index-descriptor:注释图像布局中的索引描述符。

例如,要构建一个图像,并将注释foo=bar附加到图像索引:

$ docker build --tag <IMAGE> --push --annotation "index:foo=bar" .

可以指定类型(用逗号分隔)以将注释添加到多个级别。以下示例创建一个 foo=bar在图像索引和图像清单上都带有注释的图像:

$ docker build --tag <IMAGE> --push --annotation "index,manifest:foo=bar" .

您还可以在类型前缀中指定平台限定符,以仅注释与特定操作系统和体系结构匹配的组件。以下示例foo=bar仅将注释添加到linux/amd64清单中:

$ docker build --tag <IMAGE> --push --annotation "manifest[linux/amd64]:foo=bar" .

相关文章:

参考信息: