I'm trying to get a list of the last 5 recently pushed images from AWS ECR by using ListImages. However, I observed that the ordering of the result is not what I expected.
For example:
I have an ECR repository some-service
that has the following tags: 0.1.0
, 0.2.0
... 0.6.0
, 0.6.1
and they have been being pushed in chronologically order.
When I tried to list the tags of the images, AWS ECR somehow gave me a strange ordering.
$ aws ecr list-images --repository-name some-service | jq '.imageIds[] | .imageTag'
"0.3.0"
"0.6.1"
"0.3.1"
"0.2.0"
"0.6.2"
"0.6.0"
"0.4.0"
"0.1.0"
"0.5.0"
It yielded consistently when I tried to use maxResults
:
$ aws ecr list-images --max-results 5 --repository-name some-service | jq '.imageIds[] | .imageTag'
"0.3.0"
"0.6.1"
"0.3.1"
"0.2.0"
"0.6.2"
The ordering seemed to be consistent with DescribeImages. I tried to see which data the API was using as the sort key but I could not find any.
$ aws ecr describe-images --max-result 5 \
--repository-name some-service \
| jq '.imageDetails[] | {imageTags, imageDigest, imageSizeInBytes, imagePushedAt}'
{
"imageTags": [
"0.3.0"
],
"imageDigest": "sha256:4497e8d...",
"imageSizeInBytes": 190502899,
"imagePushedAt": "2020-05-20T15:58:32+07:00"
}
{
"imageTags": [
"0.6.1"
],
"imageDigest": "sha256:45a1be8...",
"imageSizeInBytes": 218137213,
"imagePushedAt": "2020-07-27T18:48:05+07:00"
}
{
"imageTags": [
"0.3.1"
],
"imageDigest": "sha256:4b96a8ab...",
"imageSizeInBytes": 190502957,
"imagePushedAt": "2020-06-18T17:30:52+07:00"
}
{
"imageTags": [
"0.2.0"
],
"imageDigest": "sha256:168e14fb6...",
"imageSizeInBytes": 193764109,
"imagePushedAt": "2020-05-11T18:57:26+07:00"
}
{
"imageTags": [
"0.6.2"
],
"imageDigest": "sha256:2f24f...",
"imageSizeInBytes": 218146252,
"imagePushedAt": "2020-08-06T19:43:00+07:00"
}
- Does anyone know internally how AWS ECR API sorts the images?
- Is there a way to achieve what I want without using
--query
(because I'm actually using AWS Python SDK for the task).
UPDATE
I had implemented using Python and boto3. As the order was undeterministic, I had to use natsort
1 to sort the Docker Image tags.
from dataclasses import dataclass
from natsort import natsorted
@dataclass
class EcrRepository:
name: str
repository_arn: str
@dataclass
class EcrImage:
repository: EcrRepository
digest: str
tags: List[str]
__ecr_client: "boto3_ecr.Client" = get_ecr_client()
def get_last_tagged_images(ecr_repository: EcrRepository) -> List[EcrImage]:
list_image_response = __ecr_client.describe_images(
repositoryName=ecr_repository.name, filter={"tagStatus": "TAGGED"},
)
return natsorted(
[
EcrImage(
repository=ecr_repository,
digest=img.get("imageDigest"),
tags=img.get("imageTags")
)
for img in list_image_response.get("imageDetails", [])
],
key=lambda i: i.tags,
reverse=True,
)
Thanks a lot for your time!
Genzer