This utility cleans up unused Docker images on AWS Elastic Container Registry (ECR).
347
Run the following from an EC2 machine with IAM permissions which allow deleting images on ECR:
docker run --rm emindco/ecr-cleaner region repository_name images_to_keep [--delete-untagged] [--dry-run]
If you specify --delete-untagged, any untagged image in the given repository will be deleted.
You can perform a test run without actually deleting any images by specifying the --dry-run argument.
The image contains a Python script which uses boto3 to communicate with ECR. The script retrieves the current list of existing images on the given repository, sorts them (see below) by image tag and then deletes images from the beginning of the list, leaving the number of images specified in the images_to_keep argument.
The script sorts the images by image tag in a way humans expect, for example:
So, if we ran the utility with images_to_keep set to 4, we will be left with the following after the run:
This should work fine for you if your CI/CD process uses some kind of auto-incrementing naming convention for the builds. If, on the other hand, you tag your images with random stuff like commit SHAs etc., do NOT use this utility since it will delete random images.
The script skips images with the latest tag and will never delete them, regardless the specified arguments.
Content type
Image
Digest
Size
34.1 MB
Last updated
about 10 years ago
docker pull emindco/ecr-cleaner