ECR: Use docker image digests to determine ECR tags for DockerImageAsset #30937
Labels
@aws-cdk/aws-ecr
Related to Amazon Elastic Container Registry
@aws-cdk/core
Related to core CDK functionality
effort/medium
Medium work item – several days of effort
feature-request
A feature should be added or improved.
p2
Describe the feature
Either in an opt-in or default way, AWS CDK should allow using the image Digest (e.g. retrievable with
docker image ls --digest
) instead of the build context fingerprint as the AWS ECR tag.Use Case
When building a DockerImageAsset, the automatically generated tag for the asset depends on the hash of the entire build context (by using the function fingerprint). This includes files that are not included in the final generated DockerImageAsset. The image digest, on the other hand, depends on the actual bytes of the image as well as some other metadata.
This can cause unexpected redeploys of services. Consider:
src/
src/
but only copyingsrc/first-ecs-task
as well assrc/dockerfiles/first-ecs-task.Dockerfile
src/
but only copyingsrc/second-ecs-task
as well assrc/dockerfiles/second-ecs-task.Dockerfile
second-ecs-task
in ECR will show that the docker digest and image ID are identical because the actual copied bytes are identical.This behaviour is surprising, since developers generally expect a docker image to be considered the same if their contents are identical and thus not redeploy a service. Currently the solution is to pass an appropriate
excludes=
parameter or otherwise restrict the build context to just the files that will be copied. However, this entire situation could be avoided by using the docker digest directly as the tag. This could also have the benefit of reducing the amount of storage used by ECR if multiple apps reference the exact same docker image and possibly make synthesis faster since CDK doesn't need to scan the directory ahead of time.Proposed Solution
Instead of calling fingerprint() and using that to provide the tag to the docker daemon:
docker build
as usual, without a tag.Other Information
No response
Acknowledgements
CDK version used
2.136.0
Environment details (OS name and version, etc.)
Apple M1 Pro with MacOS Sonoma 14.4.1
The text was updated successfully, but these errors were encountered: