使用 Amazon 的ECR示例 AWS CLI - AWS Command Line Interface

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Amazon 的ECR示例 AWS CLI

以下代码示例向您展示如何在 Amazon 中使用来执行操作和实现常见场景ECR。 AWS Command Line Interface

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例显示了如何使用batch-check-layer-availability

AWS CLI

检查图层的可用性

以下batch-check-layer-availability示例检查cluster-autoscaler存储库中包含摘要的图层sha256:6171c7451a50945f8ddd72f7732cc04d7a0d1f48138a426b2e64387fdeb834ed的可用性。

aws ecr batch-check-layer-availability \ --repository-name cluster-autoscaler \ --layer-digests sha256:6171c7451a50945f8ddd72f7732cc04d7a0d1f48138a426b2e64387fdeb834ed

输出:

{ "layers": [ { "layerDigest": "sha256:6171c7451a50945f8ddd72f7732cc04d7a0d1f48138a426b2e64387fdeb834ed", "layerAvailability": "AVAILABLE", "layerSize": 2777, "mediaType": "application/vnd.docker.container.image.v1+json" } ], "failures": [] }

以下代码示例显示了如何使用batch-delete-image

AWS CLI

示例 1:删除图像

以下batch-delete-image示例删除了账户默认注册表precise中指定存储库中带有标签的图像。

aws ecr batch-delete-image \ --repository-name ubuntu \ --image-ids imageTag=precise

输出:

{ "failures": [], "imageIds": [ { "imageTag": "precise", "imageDigest": "sha256:19665f1e6d1e504117a1743c0a3d3753086354a38375961f2e665416ef4b1b2f" } ] }

示例 2:删除多张图片

以下batch-delete-image示例删除指定存储库team1中所有用prod和标记的图像。

aws ecr batch-delete-image \ --repository-name MyRepository \ --image-ids imageTag=prod imageTag=team1

输出:

{ "imageIds": [ { "imageDigest": "sha256:123456789012", "imageTag": "prod" }, { "imageDigest": "sha256:567890121234", "imageTag": "team1" } ], "failures": [] }

有关更多信息,请参阅 Amazon ECR 用户指南中的删除图片

以下代码示例显示了如何使用batch-get-image

AWS CLI

示例 1:获取图片

以下batch-get-image示例在账户的默认注册表v1.13.6中名为的存储库cluster-autoscaler中获取带有标签的图像。

aws ecr batch-get-image \ --repository-name cluster-autoscaler \ --image-ids imageTag=v1.13.6

输出:

{ "images": [ { "registryId": "012345678910", "repositoryName": "cluster-autoscaler", "imageId": { "imageDigest": "sha256:4a1c6567c38904384ebc64e35b7eeddd8451110c299e3368d2210066487d97e5", "imageTag": "v1.13.6" }, "imageManifest": "{\n \"schemaVersion\": 2,\n \"mediaType\": \"application/vnd.docker.distribution.manifest.v2+json\",\n \"config\": {\n \"mediaType\": \"application/vnd.docker.container.image.v1+json\",\n \"size\": 2777,\n \"digest\": \"sha256:6171c7451a50945f8ddd72f7732cc04d7a0d1f48138a426b2e64387fdeb834ed\"\n },\n \"layers\": [\n {\n \"mediaType\": \"application/vnd.docker.image.rootfs.diff.tar.gzip\",\n \"size\": 17743696,\n \"digest\": \"sha256:39fafc05754f195f134ca11ecdb1c9a691ab0848c697fffeb5a85f900caaf6e1\"\n },\n {\n \"mediaType\": \"application/vnd.docker.image.rootfs.diff.tar.gzip\",\n \"size\": 2565026,\n \"digest\": \"sha256:8c8a779d3a537b767ae1091fe6e00c2590afd16767aa6096d1b318d75494819f\"\n },\n {\n \"mediaType\": \"application/vnd.docker.image.rootfs.diff.tar.gzip\",\n \"size\": 28005981,\n \"digest\": \"sha256:c44ba47496991c9982ee493b47fd25c252caabf2b4ae7dd679c9a27b6a3c8fb7\"\n },\n {\n \"mediaType\": \"application/vnd.docker.image.rootfs.diff.tar.gzip\",\n \"size\": 775,\n \"digest\": \"sha256:e2c388b44226544363ca007be7b896bcce1baebea04da23cbd165eac30be650f\"\n }\n ]\n}" } ], "failures": [] }

示例 2:获取多张图片

以下batch-get-image示例显示了指定存储库team1中使用prod和标记的所有图像的详细信息。

aws ecr batch-get-image \ --repository-name MyRepository \ --image-ids imageTag=prod imageTag=team1

输出:

{ "images": [ { "registryId": "123456789012", "repositoryName": "MyRepository", "imageId": { "imageDigest": "sha256:123456789012", "imageTag": "prod" }, "imageManifest": "manifestExample1" }, { "registryId": "567890121234", "repositoryName": "MyRepository", "imageId": { "imageDigest": "sha256:123456789012", "imageTag": "team1" }, "imageManifest": "manifestExample2" } ], "failures": [] }

有关更多信息,请参阅 Amazon ECR 用户指南中的图片

以下代码示例显示了如何使用complete-layer-upload

AWS CLI

要完成图像图层上传

以下complete-layer-upload示例完成了向layer-test存储库的图像层上传。

aws ecr complete-layer-upload \ --repository-name layer-test \ --upload-id 6cb64b8a-9378-0e33-2ab1-b780fab8a9e9 \ --layer-digests 6cb64b8a-9378-0e33-2ab1-b780fab8a9e9:48074e6d3a68b39aad8ccc002cdad912d4148c0f92b3729323e

输出:

{ "uploadId": "6cb64b8a-9378-0e33-2ab1-b780fab8a9e9", "layerDigest": "sha256:9a77f85878aa1906f2020a0ecdf7a7e962d57e882250acd773383224b3fe9a02", "repositoryName": "layer-test", "registryId": "130757420319" }

以下代码示例显示了如何使用create-repository

AWS CLI

示例 1:创建存储库

以下create-repository示例在账户的默认注册表中的指定命名空间内创建一个存储库。

aws ecr create-repository \ --repository-name project-a/sample-repo

输出:

{ "repository": { "registryId": "123456789012", "repositoryName": "project-a/sample-repo", "repositoryArn": "arn:aws:ecr:us-west-2:123456789012:repository/project-a/sample-repo" } }

有关更多信息,请参阅 Amazon ECR 用户指南中的创建存储库

示例 2:创建配置了图像标签不可变性的存储库

以下create-repository示例在账户的默认注册表中创建了一个为标签不可变性配置的存储库。

aws ecr create-repository \ --repository-name project-a/sample-repo \ --image-tag-mutability IMMUTABLE

输出:

{ "repository": { "registryId": "123456789012", "repositoryName": "project-a/sample-repo", "repositoryArn": "arn:aws:ecr:us-west-2:123456789012:repository/project-a/sample-repo", "imageTagMutability": "IMMUTABLE" } }

有关更多信息,请参阅 Amazon ECR 用户指南中的图像标签可变性

示例 3:创建配置了扫描配置的存储库

以下create-repository示例在账户的默认注册表中创建了一个配置为在映像推送时执行漏洞扫描的存储库。

aws ecr create-repository \ --repository-name project-a/sample-repo \ --image-scanning-configuration scanOnPush=true

输出:

{ "repository": { "registryId": "123456789012", "repositoryName": "project-a/sample-repo", "repositoryArn": "arn:aws:ecr:us-west-2:123456789012:repository/project-a/sample-repo", "imageScanningConfiguration": { "scanOnPush": true } } }

有关更多信息,请参阅 Amazon ECR 用户指南中的图像扫描

以下代码示例显示了如何使用delete-lifecycle-policy

AWS CLI

删除存储库的生命周期策略

以下delete-lifecycle-policy示例删除了hello-world存储库的生命周期策略。

aws ecr delete-lifecycle-policy \ --repository-name hello-world

输出:

{ "registryId": "012345678910", "repositoryName": "hello-world", "lifecyclePolicyText": "{\"rules\":[{\"rulePriority\":1,\"description\":\"Remove untagged images.\",\"selection\":{\"tagStatus\":\"untagged\",\"countType\":\"sinceImagePushed\",\"countUnit\":\"days\",\"countNumber\":10},\"action\":{\"type\":\"expire\"}}]}", "lastEvaluatedAt": 0.0 }

以下代码示例显示了如何使用delete-repository-policy

AWS CLI

删除存储库的存储库策略

以下delete-repository-policy示例删除了存储库的cluster-autoscaler存储库策略。

aws ecr delete-repository-policy \ --repository-name cluster-autoscaler

输出:

{ "registryId": "012345678910", "repositoryName": "cluster-autoscaler", "policyText": "{\n \"Version\" : \"2008-10-17\",\n \"Statement\" : [ {\n \"Sid\" : \"allow public pull\",\n \"Effect\" : \"Allow\",\n \"Principal\" : \"*\",\n \"Action\" : [ \"ecr:BatchCheckLayerAvailability\", \"ecr:BatchGetImage\", \"ecr:GetDownloadUrlForLayer\" ]\n } ]\n}" }

以下代码示例显示了如何使用delete-repository

AWS CLI

删除存储库

以下delete-repository示例命令强制删除帐户默认注册表中的指定存储库。如果存储库包含图像,则必须使用该--force标志。

aws ecr delete-repository \ --repository-name ubuntu \ --force

输出:

{ "repository": { "registryId": "123456789012", "repositoryName": "ubuntu", "repositoryArn": "arn:aws:ecr:us-west-2:123456789012:repository/ubuntu" } }

有关更多信息,请参阅 Amazon ECR 用户指南中的删除存储库

以下代码示例显示了如何使用describe-image-scan-findings

AWS CLI

描述图像的扫描结果

以下describe-image-scan-findings示例使用账户默认注册表中指定存储库中的图像摘要返回图像扫描结果。

aws ecr describe-image-scan-findings \ --repository-name sample-repo \ --image-id imageDigest=sha256:74b2c688c700ec95a93e478cdb959737c148df3fbf5ea706abe0318726e885e6

输出:

{ "imageScanFindings": { "findings": [ { "name": "CVE-2019-5188", "description": "A code execution vulnerability exists in the directory rehashing functionality of E2fsprogs e2fsck 1.45.4. A specially crafted ext4 directory can cause an out-of-bounds write on the stack, resulting in code execution. An attacker can corrupt a partition to trigger this vulnerability.", "uri": "http://people.ubuntu.com/~ubuntu-security/cve/CVE-2019-5188", "severity": "MEDIUM", "attributes": [ { "key": "package_version", "value": "1.44.1-1ubuntu1.1" }, { "key": "package_name", "value": "e2fsprogs" }, { "key": "CVSS2_VECTOR", "value": "AV:L/AC:L/Au:N/C:P/I:P/A:P" }, { "key": "CVSS2_SCORE", "value": "4.6" } ] } ], "imageScanCompletedAt": 1579839105.0, "vulnerabilitySourceUpdatedAt": 1579811117.0, "findingSeverityCounts": { "MEDIUM": 1 } }, "registryId": "123456789012", "repositoryName": "sample-repo", "imageId": { "imageDigest": "sha256:74b2c688c700ec95a93e478cdb959737c148df3fbf5ea706abe0318726e885e6" }, "imageScanStatus": { "status": "COMPLETE", "description": "The scan was completed successfully." } }

有关更多信息,请参阅 Amazon ECR 用户指南中的图像扫描

以下代码示例显示了如何使用describe-images

AWS CLI

描述存储库中的图像

以下describe-images示例显示了cluster-autoscaler存储库中带有标签v1.13.6的图像的详细信息。

aws ecr describe-images \ --repository-name cluster-autoscaler \ --image-ids imageTag=v1.13.6

输出:

{ "imageDetails": [ { "registryId": "012345678910", "repositoryName": "cluster-autoscaler", "imageDigest": "sha256:4a1c6567c38904384ebc64e35b7eeddd8451110c299e3368d2210066487d97e5", "imageTags": [ "v1.13.6" ], "imageSizeInBytes": 48318255, "imagePushedAt": 1565128275.0 } ] }

以下代码示例显示了如何使用describe-repositories

AWS CLI

在注册表中描述存储库

此示例将描述账户默认注册表中的存储库。

命令:

aws ecr describe-repositories

输出:

{ "repositories": [ { "registryId": "012345678910", "repositoryName": "ubuntu", "repositoryArn": "arn:aws:ecr:us-west-2:012345678910:repository/ubuntu" }, { "registryId": "012345678910", "repositoryName": "test", "repositoryArn": "arn:aws:ecr:us-west-2:012345678910:repository/test" } ] }

以下代码示例显示了如何使用get-authorization-token

AWS CLI

获取默认注册表的授权令牌

以下get-authorization-token示例命令获取默认注册表的授权令牌。

aws ecr get-authorization-token

输出:

{ "authorizationData": [ { "authorizationToken": "QVdTOkN...", "expiresAt": 1448875853.241, "proxyEndpoint": "https://123456789012.dkr.ecr.us-west-2.amazonaws.com" } ] }

以下代码示例显示了如何使用get-download-url-for-layer

AWS CLI

获取图层URL的下载

以下get-download-url-for-layer示例显示了cluster-autoscaler存储库中包含摘要的图层sha256:6171c7451a50945f8ddd72f7732cc04d7a0d1f48138a426b2e64387fdeb834ed的下载URL情况。

aws ecr get-download-url-for-layer \ --repository-name cluster-autoscaler \ --layer-digest sha256:6171c7451a50945f8ddd72f7732cc04d7a0d1f48138a426b2e64387fdeb834ed

输出:

{ "downloadUrl": "https://prod-us-west-2-starport-layer-bucket.s3.us-west-2.amazonaws.com/e501-012345678910-9cb60dc0-7284-5643-3987-da6dac0465f0/04620aac-66a5-4167-8232-55ee7ef6d565?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20190814T220617Z&X-Amz-SignedHeaders=host&X-Amz-Expires=3600&X-Amz-Credential=AKIA32P3D2JDNMVAJLGF%2F20190814%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Signature=9161345894947a1672467a0da7a1550f2f7157318312fe4941b59976239c3337", "layerDigest": "sha256:6171c7451a50945f8ddd72f7732cc04d7a0d1f48138a426b2e64387fdeb834ed" }

以下代码示例显示了如何使用get-lifecycle-policy-preview

AWS CLI

检索生命周期策略预览的详细信息

以下get-lifecycle-policy-preview示例检索账户的默认注册表中指定存储库的生命周期策略预览结果。

命令:

aws ecr get-lifecycle-policy-preview \ --repository-name "project-a/amazon-ecs-sample"

输出:

{ "registryId": "012345678910", "repositoryName": "project-a/amazon-ecs-sample", "lifecyclePolicyText": "{\n \"rules\": [\n {\n \"rulePriority\": 1,\n \"description\": \"Expire images older than 14 days\",\n \"selection\": {\n \"tagStatus\": \"untagged\",\n \"countType\": \"sinceImagePushed\",\n \"countUnit\": \"days\",\n \"countNumber\": 14\n },\n \"action\": {\n \"type\": \"expire\"\n }\n }\n ]\n}\n", "status": "COMPLETE", "previewResults": [], "summary": { "expiringImageTotalCount": 0 } }

有关更多信息,请参阅 Amazon ECR 用户指南中的生命周期政策

以下代码示例显示了如何使用get-lifecycle-policy

AWS CLI

检索生命周期策略

以下get-lifecycle-policy示例在账户的默认注册表中显示指定存储库的生命周期策略的详细信息。

aws ecr get-lifecycle-policy \ --repository-name "project-a/amazon-ecs-sample"

输出:

{ "registryId": "123456789012", "repositoryName": "project-a/amazon-ecs-sample", "lifecyclePolicyText": "{\"rules\":[{\"rulePriority\":1,\"description\":\"Expire images older than 14 days\",\"selection\":{\"tagStatus\":\"untagged\",\"countType\":\"sinceImagePushed\",\"countUnit\":\"days\",\"countNumber\":14},\"action\":{\"type\":\"expire\"}}]}", "lastEvaluatedAt": 1504295007.0 }

有关更多信息,请参阅 Amazon ECR 用户指南中的生命周期政策

以下代码示例显示了如何使用get-login-password

AWS CLI

检索密码以向注册表进行身份验证

下面get-login-password显示了一个密码,您可以将该密码与您选择的容器客户端一起使用,对IAM委托人有权访问的任何 Amazon ECR 注册表进行身份验证。

aws ecr get-login-password

输出:

<password>

要与 Docker 一起使用CLI,请将命令的输出通过管道get-login-password传递给该docker login命令。检索密码时,请确保您指定的区域与您的 Amazon ECR 注册所在的区域相同。

aws ecr get-login-password \ --region <region> \ | docker login \ --username AWS \ --password-stdin <aws_account_id>.dkr.ecr.<region>.amazonaws.com

有关更多信息,请参阅 Amazon ECR 用户指南中的注册身份验证

以下代码示例显示了如何使用get-login

AWS CLI

将 Docker 登录命令检索到您的默认注册表

此示例打印了一个可用于登录默认 Amazon ECR 注册表的命令。

命令:

aws ecr get-login

输出:

docker login -u AWS -p <password> -e none https://<aws_account_id>.dkr.ecr.<region>.amazonaws.com

登录其他账户的注册表

此示例打印了一个或多个命令,您可以使用这些命令登录与其他账户关联的 Amazon ECR 注册表。

命令:

aws ecr get-login --registry-ids 012345678910 023456789012

输出:

docker login -u <username> -p <token-1> -e none <endpoint-1> docker login -u <username> -p <token-2> -e none <endpoint-2>

以下代码示例显示了如何使用get-repository-policy

AWS CLI

检索存储库的存储库策略

以下get-repository-policy示例显示了有关存储库存储库策略的cluster-autoscaler详细信息。

aws ecr get-repository-policy \ --repository-name cluster-autoscaler

输出:

{ "registryId": "012345678910", "repositoryName": "cluster-autoscaler", "policyText": "{\n \"Version\" : \"2008-10-17\",\n \"Statement\" : [ {\n \"Sid\" : \"allow public pull\",\n \"Effect\" : \"Allow\",\n \"Principal\" : \"*\",\n \"Action\" : [ \"ecr:BatchCheckLayerAvailability\", \"ecr:BatchGetImage\", \"ecr:GetDownloadUrlForLayer\" ]\n } ]\n}" }

以下代码示例显示了如何使用initiate-layer-upload

AWS CLI

启动图像图层上传

以下initiate-layer-upload示例启动图像层上传到layer-test存储库。

aws ecr initiate-layer-upload \ --repository-name layer-test

输出:

{ "partSize": 10485760, "uploadId": "6cb64b8a-9378-0e33-2ab1-b780fab8a9e9" }

以下代码示例显示了如何使用list-images

AWS CLI

列出存储库的映像

以下 list-images 示例将显示 cluster-autoscaler 存储库的映像列表。

aws ecr list-images \ --repository-name cluster-autoscaler

输出:

{ "imageIds": [ { "imageDigest": "sha256:99c6fb4377e9a420a1eb3b410a951c9f464eff3b7dbc76c65e434e39b94b6570", "imageTag": "v1.13.8" }, { "imageDigest": "sha256:99c6fb4377e9a420a1eb3b410a951c9f464eff3b7dbc76c65e434e39b94b6570", "imageTag": "v1.13.7" }, { "imageDigest": "sha256:4a1c6567c38904384ebc64e35b7eeddd8451110c299e3368d2210066487d97e5", "imageTag": "v1.13.6" } ] }

以下代码示例显示了如何使用list-tags-for-resource

AWS CLI

列出存储库的标签

以下list-tags-for-resource示例显示了与hello-world存储库关联的标签列表。

aws ecr list-tags-for-resource \ --resource-arn arn:aws:ecr:us-west-2:012345678910:repository/hello-world

输出:

{ "tags": [ { "Key": "Stage", "Value": "Integ" } ] }

以下代码示例显示了如何使用put-image-scanning-configuration

AWS CLI

更新存储库的图像扫描配置

以下put-image-scanning-configuration示例更新了指定存储库的图像扫描配置。

aws ecr put-image-scanning-configuration \ --repository-name sample-repo \ --image-scanning-configuration scanOnPush=true

输出:

{ "registryId": "012345678910", "repositoryName": "sample-repo", "imageScanningConfiguration": { "scanOnPush": true } }

有关更多信息,请参阅 Amazon ECR 用户指南中的图像扫描

以下代码示例显示了如何使用put-image-tag-mutability

AWS CLI

更新存储库的图像标签可变性设置

以下put-image-tag-mutability示例为标签不可变性配置了指定的存储库。这样可以防止存储库中的所有图像标签被覆盖。

aws ecr put-image-tag-mutability \ --repository-name hello-repository \ --image-tag-mutability IMMUTABLE

输出:

{ "registryId": "012345678910", "repositoryName": "sample-repo", "imageTagMutability": "IMMUTABLE" }

有关更多信息,请参阅 Amazon ECR 用户指南中的图像标签可变性

以下代码示例显示了如何使用put-image

AWS CLI

使用清单重新标记图像

以下put-image示例使用现有图像清单在hello-world存储库中创建一个新标签。

aws ecr put-image \ --repository-name hello-world \ --image-tag 2019.08 \ --image-manifest file://hello-world.manifest.json

hello-world.manifest.json 的内容:

{ "schemaVersion": 2, "mediaType": "application/vnd.docker.distribution.manifest.v2+json", "config": { "mediaType": "application/vnd.docker.container.image.v1+json", "size": 5695, "digest": "sha256:cea5fe7701b7db3dd1c372f3cea6f43cdda444fcc488f530829145e426d8b980" }, "layers": [ { "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", "size": 39096921, "digest": "sha256:d8868e50ac4c7104d2200d42f432b661b2da8c1e417ccfae217e6a1e04bb9295" }, { "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", "size": 57938, "digest": "sha256:83251ac64627fc331584f6c498b3aba5badc01574e2c70b2499af3af16630eed" }, { "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", "size": 423, "digest": "sha256:589bba2f1b36ae56f0152c246e2541c5aa604b058febfcf2be32e9a304fec610" }, { "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", "size": 680, "digest": "sha256:d62ecaceda3964b735cdd2af613d6bb136a52c1da0838b2ff4b4dab4212bcb1c" }, { "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", "size": 162, "digest": "sha256:6d93b41cfc6bf0d2522b7cf61588de4cd045065b36c52bd3aec2ba0622b2b22b" }, { "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", "size": 28268840, "digest": "sha256:6986b4d4c07932c680b3587f2eac8b0e013568c003cc23b04044628a5c5e599f" }, { "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", "size": 35369152, "digest": "sha256:8c5ec60f10102dc8da0649d866c7c2f706e459d0bdc25c83ad2de86f4996c276" }, { "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", "size": 155, "digest": "sha256:cde50b1c594539c5f67cbede9aef95c9ae321ccfb857f7b251b45b84198adc85" }, { "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", "size": 28737, "digest": "sha256:2e102807ab72a73fc9abf53e8c50e421bdc337a0a8afcb242176edeec65977e4" }, { "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", "size": 190, "digest": "sha256:fc379bbd5ed37808772bef016553a297356c59b8f134659e6ee4ecb563c2f5a7" }, { "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", "size": 28748, "digest": "sha256:021db240dfccf5a1aff19507d17c0177e5888e518acf295b52204b1825e8b7ee" } ] }

输出:

{ "image": { "registryId": "130757420319", "repositoryName": "hello-world", "imageId": { "imageDigest": "sha256:8ece96b74f87652876199d83bd107d0435a196133af383ac54cb82b6cc5283ae", "imageTag": "2019.08" }, "imageManifest": "{\n \"schemaVersion\": 2,\n \"mediaType\": \"application/vnd.docker.distribution.manifest.v2+json\",\n \"config\": {\n \"mediaType\": \"application/vnd.docker.container.image.v1+json\",\n \"size\": 5695,\n \"digest\": \"sha256:cea5fe7701b7db3dd1c372f3cea6f43cdda444fcc488f530829145e426d8b980\"\n },\n \"layers\": [\n {\n \"mediaType\": \"application/vnd.docker.image.rootfs.diff.tar.gzip\",\n \"size\": 39096921,\n \"digest\": \"sha256:d8868e50ac4c7104d2200d42f432b661b2da8c1e417ccfae217e6a1e04bb9295\"\n },\n {\n \"mediaType\": \"application/vnd.docker.image.rootfs.diff.tar.gzip\",\n \"size\": 57938,\n \"digest\": \"sha256:83251ac64627fc331584f6c498b3aba5badc01574e2c70b2499af3af16630eed\"\n },\n {\n \"mediaType\": \"application/vnd.docker.image.rootfs.diff.tar.gzip\",\n \"size\": 423,\n \"digest\": \"sha256:589bba2f1b36ae56f0152c246e2541c5aa604b058febfcf2be32e9a304fec610\"\n },\n {\n \"mediaType\": \"application/vnd.docker.image.rootfs.diff.tar.gzip\",\n \"size\": 680,\n \"digest\": \"sha256:d62ecaceda3964b735cdd2af613d6bb136a52c1da0838b2ff4b4dab4212bcb1c\"\n },\n {\n \"mediaType\": \"application/vnd.docker.image.rootfs.diff.tar.gzip\",\n \"size\": 162,\n \"digest\": \"sha256:6d93b41cfc6bf0d2522b7cf61588de4cd045065b36c52bd3aec2ba0622b2b22b\"\n },\n {\n \"mediaType\": \"application/vnd.docker.image.rootfs.diff.tar.gzip\",\n \"size\": 28268840,\n \"digest\": \"sha256:6986b4d4c07932c680b3587f2eac8b0e013568c003cc23b04044628a5c5e599f\"\n },\n {\n \"mediaType\": \"application/vnd.docker.image.rootfs.diff.tar.gzip\",\n \"size\": 35369152,\n \"digest\": \"sha256:8c5ec60f10102dc8da0649d866c7c2f706e459d0bdc25c83ad2de86f4996c276\"\n },\n {\n \"mediaType\": \"application/vnd.docker.image.rootfs.diff.tar.gzip\",\n \"size\": 155,\n \"digest\": \"sha256:cde50b1c594539c5f67cbede9aef95c9ae321ccfb857f7b251b45b84198adc85\"\n },\n {\n \"mediaType\": \"application/vnd.docker.image.rootfs.diff.tar.gzip\",\n \"size\": 28737,\n \"digest\": \"sha256:2e102807ab72a73fc9abf53e8c50e421bdc337a0a8afcb242176edeec65977e4\"\n },\n {\n \"mediaType\": \"application/vnd.docker.image.rootfs.diff.tar.gzip\",\n \"size\": 190,\n \"digest\": \"sha256:fc379bbd5ed37808772bef016553a297356c59b8f134659e6ee4ecb563c2f5a7\"\n },\n {\n \"mediaType\": \"application/vnd.docker.image.rootfs.diff.tar.gzip\",\n \"size\": 28748,\n \"digest\": \"sha256:021db240dfccf5a1aff19507d17c0177e5888e518acf295b52204b1825e8b7ee\"\n }\n ]\n}\n" } }

以下代码示例显示了如何使用put-lifecycle-policy

AWS CLI

创建生命周期策略

以下put-lifecycle-policy示例在账户的默认注册表中为指定存储库创建生命周期策略。

aws ecr put-lifecycle-policy \ --repository-name "project-a/amazon-ecs-sample" \ --lifecycle-policy-text "file://policy.json"

policy.json 的内容:

{ "rules": [ { "rulePriority": 1, "description": "Expire images older than 14 days", "selection": { "tagStatus": "untagged", "countType": "sinceImagePushed", "countUnit": "days", "countNumber": 14 }, "action": { "type": "expire" } } ] }

输出:

{ "registryId": "<aws_account_id>", "repositoryName": "project-a/amazon-ecs-sample", "lifecyclePolicyText": "{\"rules\":[{\"rulePriority\":1,\"description\":\"Expire images older than 14 days\",\"selection\":{\"tagStatus\":\"untagged\",\"countType\":\"sinceImagePushed\",\"countUnit\":\"days\",\"countNumber\":14},\"action\":{\"type\":\"expire\"}}]}" }

有关更多信息,请参阅 Amazon ECR 用户指南中的生命周期政策

以下代码示例显示了如何使用set-repository-policy

AWS CLI

为存储库设置存储库策略

以下set-repository-policy示例将文件中包含的存储库策略附加到cluster-autoscaler存储库。

aws ecr set-repository-policy \ --repository-name cluster-autoscaler \ --policy-text file://my-policy.json

my-policy.json 的内容:

{ "Version" : "2008-10-17", "Statement" : [ { "Sid" : "allow public pull", "Effect" : "Allow", "Principal" : "*", "Action" : [ "ecr:BatchCheckLayerAvailability", "ecr:BatchGetImage", "ecr:GetDownloadUrlForLayer" ] } ] }

输出:

{ "registryId": "012345678910", "repositoryName": "cluster-autoscaler", "policyText": "{\n \"Version\" : \"2008-10-17\",\n \"Statement\" : [ {\n \"Sid\" : \"allow public pull\",\n \"Effect\" : \"Allow\",\n \"Principal\" : \"*\",\n \"Action\" : [ \"ecr:BatchCheckLayerAvailability\", \"ecr:BatchGetImage\", \"ecr:GetDownloadUrlForLayer\" ]\n } ]\n}" }

以下代码示例显示了如何使用start-image-scan

AWS CLI

启动图像漏洞扫描

以下start-image-scan示例启动图像扫描,并由指定存储库中的图像摘要指定。

aws ecr start-image-scan \ --repository-name sample-repo \ --image-id imageDigest=sha256:74b2c688c700ec95a93e478cdb959737c148df3fbf5ea706abe0318726e885e6

输出:

{ "registryId": "012345678910", "repositoryName": "sample-repo", "imageId": { "imageDigest": "sha256:74b2c688c700ec95a93e478cdb959737c148df3fbf5ea706abe0318726e885e6" }, "imageScanStatus": { "status": "IN_PROGRESS" } }

有关更多信息,请参阅 Amazon ECR 用户指南中的图像扫描

以下代码示例显示了如何使用start-lifecycle-policy-preview

AWS CLI

创建生命周期策略预览

以下start-lifecycle-policy-preview示例为指定存储库创建由JSON文件定义的生命周期策略预览。

aws ecr start-lifecycle-policy-preview \ --repository-name "project-a/amazon-ecs-sample" \ --lifecycle-policy-text "file://policy.json"

policy.json 的内容:

{ "rules": [ { "rulePriority": 1, "description": "Expire images older than 14 days", "selection": { "tagStatus": "untagged", "countType": "sinceImagePushed", "countUnit": "days", "countNumber": 14 }, "action": { "type": "expire" } } ] }

输出:

{ "registryId": "012345678910", "repositoryName": "project-a/amazon-ecs-sample", "lifecyclePolicyText": "{\n \"rules\": [\n {\n \"rulePriority\": 1,\n \"description\": \"Expire images older than 14 days\",\n \"selection\": {\n \"tagStatus\": \"untagged\",\n \"countType\": \"sinceImagePushed\",\n \"countUnit\": \"days\",\n \"countNumber\": 14\n },\n \"action\": {\n \"type\": \"expire\"\n }\n }\n ]\n}\n", "status": "IN_PROGRESS" }

以下代码示例显示了如何使用tag-resource

AWS CLI

为存储库添加标签

以下tag-resource示例在hello-world存储库Integ上设置了一个带有键Stage和值的标签。

aws ecr tag-resource \ --resource-arn arn:aws:ecr:us-west-2:012345678910:repository/hello-world \ --tags Key=Stage,Value=Integ

此命令不生成任何输出。

以下代码示例显示了如何使用untag-resource

AWS CLI

取消存储库的标签

以下untag-resource示例Stagehello-world存储库中删除带有密钥的标签。

aws ecr untag-resource \ --resource-arn arn:aws:ecr:us-west-2:012345678910:repository/hello-world \ --tag-keys Stage

此命令不生成任何输出。

以下代码示例显示了如何使用upload-layer-part

AWS CLI

上传图层分段

以下内容upload-layer-part将图像层部分上传到layer-test存储库。

aws ecr upload-layer-part \ --repository-name layer-test \ --upload-id 6cb64b8a-9378-0e33-2ab1-b780fab8a9e9 \ --part-first-byte 0 \ --part-last-byte 8323314 \ --layer-part-blob file:///var/lib/docker/image/overlay2/layerdb/sha256/ff986b10a018b48074e6d3a68b39aad8ccc002cdad912d4148c0f92b3729323e/layer.b64

输出:

{ "uploadId": "6cb64b8a-9378-0e33-2ab1-b780fab8a9e9", "registryId": "012345678910", "lastByteReceived": 8323314, "repositoryName": "layer-test" }