如何使用 docker scout attestation add 命令为镜像附加证明

DockerDockerBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在这个实验中,你将学习如何使用 docker scout attestation add 命令为 Docker 镜像添加证明(attestation)。证明是有价值的元数据,它可以提供有关镜像来源、构建过程、安全扫描结果等关键信息。

你将首先准备一个 Docker 镜像和一个简单的证明文件。然后,你将使用 docker scout attestation add 命令并搭配 --file 标志,将证明附加到镜像上。你还将探索如何使用 --predicate-type 标志为证明指定特定的谓词类型(predicate type)。最后,你将验证证明是否已成功添加到镜像中。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker(("Docker")) -.-> docker/DockerfileGroup(["Dockerfile"]) docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/inspect("Inspect Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") docker/ImageOperationsGroup -.-> docker/images("List Images") docker/DockerfileGroup -.-> docker/build("Build Image from Dockerfile") subgraph Lab Skills docker/ls -.-> lab-555197{{"如何使用 docker scout attestation add 命令为镜像附加证明"}} docker/inspect -.-> lab-555197{{"如何使用 docker scout attestation add 命令为镜像附加证明"}} docker/pull -.-> lab-555197{{"如何使用 docker scout attestation add 命令为镜像附加证明"}} docker/images -.-> lab-555197{{"如何使用 docker scout attestation add 命令为镜像附加证明"}} docker/build -.-> lab-555197{{"如何使用 docker scout attestation add 命令为镜像附加证明"}} end

准备镜像和证明文件

在这一步中,我们将准备一个 Docker 镜像和一个证明文件。证明是一种元数据,它提供有关镜像的信息,例如镜像的构建方式、漏洞扫描情况或签名信息。在这个示例中,我们将使用一个简单的文本文件作为证明文件。

首先,让我们拉取一个我们将使用的基础 Docker 镜像。我们将使用 alpine 镜像,它是一个轻量级的 Linux 发行版。

docker pull alpine:latest

你应该会看到表明镜像正在被拉取和下载的输出。

latest: Pulling from library/alpine
...
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest

接下来,我们将创建一个简单的证明文件。这个文件将包含一些任意的 JSON 数据。在实际场景中,这个文件会包含有关镜像的结构化数据,例如构建信息、扫描结果或签名细节。

我们将在你的 ~/project 目录下创建一个名为 attestation.json 的文件。

nano ~/project/attestation.json

nano 编辑器中,粘贴以下 JSON 内容:

{
  "predicateType": "https://example.com/attestation/v1",
  "data": {
    "key": "value",
    "another_key": 123
  }
}

Ctrl + X,然后按 Y,再按 Enter 保存文件。

你可以使用 cat 命令验证文件的内容:

cat ~/project/attestation.json

输出应该是你刚刚粘贴的 JSON 内容。

现在,你已经准备好了 Docker 镜像 (alpine:latest) 和证明文件 (~/project/attestation.json),可以进行下一步了。

使用 --file 为镜像添加证明

在这一步中,我们将把上一步创建的证明文件添加到 alpine:latest 镜像中。我们将使用 docker buildx imagetools create 命令,并搭配 --attestation-file 标志。这个命令允许我们创建一个包含额外元数据(包括证明)的新镜像清单(image manifest)。

添加证明文件的基本语法如下:

docker buildx imagetools create <source_image> --attestation-file <attestation_file_path> --tag <new_image_tag>

这里,<source_image> 是你要添加证明的镜像,<attestation_file_path> 是你的证明文件的路径,<new_image_tag> 是包含证明的新镜像清单的标签。

让我们将 ~/project/attestation.json 文件添加到 alpine:latest 镜像中,并将新镜像标记为 alpine:attested

docker buildx imagetools create alpine:latest --attestation-file ~/project/attestation.json --tag alpine:attested

你应该会看到表明正在创建并推送新镜像清单的输出。

...
unpacking to docker.io/library/alpine:attested done

这个命令创建了一个带有 alpine:attested 标签的新镜像清单,其中包含了来自 ~/project/attestation.json 的证明数据。原始的 alpine:latest 镜像保持不变。

现在,你可以列出你的 Docker 镜像,查看新创建的 alpine:attested 镜像。

docker images

你应该会在列表中看到 alpine:latestalpine:attested

REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
alpine       attested  <image_id>     ...            ...
alpine       latest    <image_id>     ...            ...
...

请注意,两个镜像的 IMAGE ID 可能相同,因为 imagetools create 通常会复用底层的镜像层,只是创建一个新的清单。

使用 --predicate-type 添加具有特定谓词类型(predicate type)的证明

在上一步中,我们使用 --attestation-file 标志添加了一个证明。Docker 会自动从 JSON 文件中的 predicateType 字段推断出谓词类型。不过,你也可以使用 --predicate-type 标志显式指定谓词类型。如果你的证明数据中不包含 predicateType 字段,或者你想覆盖文件中的该字段,这种方法就很有用。

添加具有特定谓词类型的证明的基本语法如下:

docker buildx imagetools create <source_image> --attestation-file <attestation_file_path> --predicate-type <predicate_type_url> --tag <new_image_tag>

这里,<predicate_type_url> 是表示证明类型的 URL。

让我们再次将相同的 ~/project/attestation.json 文件添加到 alpine:latest 镜像中,但这次我们将显式地将谓词类型设置为 https://example.com/another-attestation/v2,并将新镜像标记为 alpine:attested-v2

docker buildx imagetools create alpine:latest --attestation-file ~/project/attestation.json --predicate-type https://example.com/another-attestation/v2 --tag alpine:attested-v2

你应该会看到与上一步类似的输出,表明正在创建一个新的镜像清单。

...
unpacking to docker.io/library/alpine:attested-v2 done

这个命令创建了一个带有 alpine:attested-v2 标签的新镜像清单。尽管 attestation.json 文件中包含 "predicateType": "https://example.com/attestation/v1",但 --predicate-type 标志会覆盖它,新清单中的证明将具有谓词类型 https://example.com/another-attestation/v2

让我们再次列出镜像,查看新标签。

docker images

现在你应该会看到 alpine:latestalpine:attestedalpine:attested-v2

REPOSITORY   TAG           IMAGE ID       CREATED        SIZE
alpine       attested-v2   <image_id>     ...            ...
alpine       attested      <image_id>     ...            ...
alpine       latest        <image_id>     ...            ...
...

在下一步中,我们将验证证明是否已正确添加,并检查其内容。

验证添加的证明

在这最后一步中,我们将验证证明是否已成功添加到镜像中,并检查其内容。你可以使用 docker buildx imagetools inspect 命令查看镜像的清单,其中包含了任何相关证明的信息。

检查镜像清单的基本语法如下:

docker buildx imagetools inspect <image_tag>

首先,让我们检查使用 --attestation-file 标志创建的 alpine:attested 镜像。

docker buildx imagetools inspect alpine:attested

你应该会看到关于镜像清单的详细输出。查找与“attestations”(证明)相关的部分。你应该能找到一个谓词类型为 https://example.com/attestation/v1 的条目,以及来自 attestation.json 文件的数据。

...
  "attestations": [
    {
      "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
      "payload": "...",
      "predicateType": "https://example.com/attestation/v1"
    }
  ],
...

现在,让我们检查使用 --predicate-type 标志覆盖谓词类型后创建的 alpine:attested-v2 镜像。

docker buildx imagetools inspect alpine:attested-v2

同样,查找“attestations”部分。这次,你应该会看到一个谓词类型为 https://example.com/another-attestation/v2 的条目,尽管证明数据的内容与之前相同。

...
  "attestations": [
    {
      "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
      "payload": "...",
      "predicateType": "https://example.com/another-attestation/v2"
    }
  ],
...

这证实了我们已成功将证明添加到 Docker 镜像中,并且可以使用 --predicate-type 标志显式设置谓词类型。

现在,你已经学会了如何准备镜像和证明文件,如何使用 --attestation-file--predicate-type 标志将证明添加到镜像中,以及如何使用 docker buildx imagetools inspect 验证添加的证明。

总结

在本次实验中,我们学习了如何使用 docker scout attestation add 命令为 Docker 镜像附加证明。首先,我们准备了一个 Docker 镜像(alpine:latest)并创建了一个简单的 JSON 证明文件。然后,我们使用带有 --file 标志的 docker scout attestation add 命令将证明文件附加到镜像上。我们还探讨了如何使用 --predicate-type 标志为证明指定自定义的谓词类型(predicate type)。最后,我们验证了证明已成功添加到镜像中。