![](/img/trans.png)
[英]How to delete aws ECR repository which contain images through cloudformation?
[英]How to delete untagged images from AWS ECR Container Registry
将图像推送到 Amazon ECR 时,如果标签已存在于存储库中,则旧图像仍保留在注册表中,但进入未标记的 state。
因此,如果我 docker push image/haha:1.0.0
我第二次执行此操作(前提是发生了某些变化),则第一张图像从AWS ECR
中取消标记。
有没有办法安全地清除未标记图像中的所有注册表?
您可以在单个请求中删除所有图像,无需循环:
IMAGES_TO_DELETE=$( aws ecr list-images --region $ECR_REGION --repository-name $ECR_REPO --filter "tagStatus=UNTAGGED" --query 'imageIds[*]' --output json )
aws ecr batch-delete-image --region $ECR_REGION --repository-name $ECR_REPO --image-ids "$IMAGES_TO_DELETE" || true
首先,它以json格式获取未标记的图像列表:
[ {"imageDigest": "sha256:..."}, {"imageDigest": "sha256:..."}, ... ]
然后它将该列表发送到batch-image-delete
。
最后一个|| true
当没有未标记的图像时,需要|| true
以避免错误代码。
现在,ECR 支持生命周期策略 ( https://docs.aws.amazon.com/AmazonECR/latest/userguide/LifecyclePolicies.html ),您可以使用它自动删除未标记的图像。
使用控制台设置生命周期策略预览
通过https://console.aws.amazon.com/ecs/打开 Amazon ECS 控制台。
从导航栏中,选择包含要对其执行生命周期策略预览的存储库的区域。
在导航窗格中,选择存储库并选择一个存储库。
在所有存储库:存储库名称页面上,选择试运行生命周期规则、添加。
为您的生命周期策略规则输入以下详细信息:
对于规则优先级,键入规则优先级的数字。
对于规则描述,键入生命周期策略规则的描述。
对于图像状态,选择标记或未标记。
如果您为图像状态指定了标记,那么对于标签前缀列表,您可以选择指定一个图像标签列表,以根据您的生命周期策略对其执行操作。 如果您指定未标记,则此字段必须为空。
对于匹配条件,选择计数类型、计数编号和计数单位(如果适用)的值。
选择保存
通过重复步骤 5-7 创建其他生命周期策略规则。
要运行生命周期策略预览,请选择 Save and preview results。
在预览图像结果下,查看生命周期策略预览的影响。
如果您对预览结果感到满意,请选择应用为生命周期策略以创建具有指定规则的生命周期策略。
从这里: https : //docs.aws.amazon.com/AmazonECR/latest/userguide/lpp_creation.html
我实际上使用aws cli
伪造了一个单行解决方案
aws ecr describe-repositories --output text | awk '{print $5}' | while read line; do aws ecr list-images --repository-name $line --filter tagStatus=UNTAGGED --query 'imageIds[*]' --output text | while read imageId; do aws ecr batch-delete-image --repository-name $line --image-ids imageDigest=$imageId; done; done
它的作用是:
tagStatus=UNTAGGED
图像batch-delete-image
设置生命周期策略绝对是管理此问题的最佳方式。 话虽如此 - 如果您确实有一堆要删除的图像,请记住,batch-delete-images 的最大值为 100。所以您需要这样做是因为未标记的图像数量大于 100:
IMAGES_TO_DELETE=$( aws ecr list-images --repository-name $ECR_REPO --filter "tagStatus=UNTAGGED" --query 'imageIds[0:100]' --output json )
echo $IMAGES_TO_DELETE | jq length # Gets the number of results
aws ecr batch-delete-image --repository-name $ECR_REPO --image-ids "$IMAGES_TO_DELETE" --profile qa || true
基于@Ken J 的回答,
这是一个可以清理所有 ECR 的 python 脚本:
#!/usr/bin/python3
import subprocess
import json
import os
# Based on: https://stackoverflow.com/questions/40949342/how-to-delete-untagged-images-from-aws-ecr-container-registry
region="us-east-1"
debug = False
def _runCommand(command):
if debug:
print(" ".join(command))
p = subprocess.Popen(command, shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
return [p.stdout.read().decode("utf-8"), p.stderr.read().decode("utf-8")]
command = "aws ecr describe-repositories --region " + region + " --output json".split(" ")
data = _runCommand(command)[0]
for i in json.loads(data)["repositories"]:
name = i["repositoryName"]
print(name)
command = ["aws", "ecr", "list-images", "--region", region, "--repository-name", name, "--filter", "tagStatus=UNTAGGED", "--query", 'imageIds[*]', "--output" , "json"]
data = _runCommand(command)[0]
command = ["aws", "ecr", "batch-delete-image", "--region", region, "--repository-name", name, "--image-ids",data]
data = _runCommand(command)[0]
print(data)
如果您想从存储库中删除未标记的图像,您只需创建一个 JSON 生命周期策略,然后使用 python 将 JSON 策略应用于存储库
就我而言,我正在将策略应用于 ECR 中的所有 ECR 存储库,并且我在当前目录中创建了一个“lifecyclepolicy.json”文件,我在其中添加了 ECR 的生命周期策略
这是我的python代码:-
import os
import json
import boto3
def ecr_lifecycle(lifecycle_policy):
ecr_client = boto3.client('ecr')
repositories = []
describe_repo_paginator = ecr_client.get_paginator('describe_repositories')
for response_list_repopaginator in describe_repo_paginator.paginate():
for repo in response_list_repopaginator['repositories']:
repositories.append(repo['repositoryName'])
for repository in repositories:
response=ecr_client.put_lifecycle_policy(repositoryName=repository,
lifecyclePolicyText=json.dumps(lifecycle_policy))
return response
if __name__ == '__main__':
path = os.path.dirname(__file__)
json_file = open(os.path.join(path, 'lifecyclepolicy.json'))
data = json.load(json_file)
ecr_lifecycle(data)
如果您想查看 JSON 文件:-
{
"rules": [
{
{
"rulePriority": 10,
"description": "Only keep untagged images for 7 days",
"selection": {
"tagStatus": "untagged",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 7
}
"action": {
"type": "expire"
}
}
]
}
untaggedImages = aws ecr list-images --repository-name <your_repo_name> --filter "tagStatus=UNTAGGED" --query 'to_string(imageIds[*])' --output json""")
aws ecr batch-delete-image --repository-name <your_repo_name> --image-ids "$untaggedImages" || true """)
to_string function 是必需的,因为返回的 JSON 不是字符串格式,而是 Object。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.