Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ECR: Use docker image digests to determine ECR tags for DockerImageAsset #30937

Open
2 tasks
skairunner opened this issue Jul 24, 2024 · 1 comment
Open
2 tasks
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

Comments

@skairunner
Copy link

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:

  • All code is stored in src/
  • One ECS task builds a docker image, passing the directory src/ but only copying src/first-ecs-task as well as src/dockerfiles/first-ecs-task.Dockerfile
  • Another ECS task builds a docker image, passing the directory src/ but only copying src/second-ecs-task as well as src/dockerfiles/second-ecs-task.Dockerfile
  • If the first ecs task's source code is modified, both services will be redeployed since the build context has changed. However, checking the old and new images for 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:

  1. Invoke docker build as usual, without a tag.
  2. The final line of the process should provide an image ID. Use that image ID to fetch the digest.
  3. Tag the image with the digest and push to ECR.

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.136.0

Environment details (OS name and version, etc.)

Apple M1 Pro with MacOS Sonoma 14.4.1

@skairunner skairunner added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Jul 24, 2024
@github-actions github-actions bot added the @aws-cdk/aws-ecr Related to Amazon Elastic Container Registry label Jul 24, 2024
@pahud
Copy link
Contributor

pahud commented Jul 24, 2024

Thank you for your insightful feedback. Yes it could happen when multiple image assets are sharing the same directory with different file assets. Before we have that implementation, I guess we need to separate the src directories for different image assets and I agree that's not a good idea. I am making this a p2 feature request and I'll bring this up to the team.

@pahud pahud added p2 @aws-cdk/core Related to core CDK functionality effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

2 participants