(cdk-assets): Support Docker BuildKit --push
during DockerImageAsset publishing
#29768
Closed
2 tasks
Labels
@aws-cdk/aws-ecr
Related to Amazon Elastic Container Registry
effort/medium
Medium work item – several days of effort
feature-request
A feature should be added or improved.
p2
Describe the feature
Today, CDK uses three distinct calls to
docker
to build, tag, and push images.This three-step behavior was the standard workflow for the docker (e.g., non-BuildKit driver). However, with the introduction of Docker BuildKit, the preferred workflow is to use the
--push
flag during thedocker build
command. This prevents needing to export the image from the BuildKit container to the Docker daemon, which is slow, especially on CI platforms.When you build a Docker image using BuildKit without specifying the
--push
flag, you'll see a message that looks like this:Indeed, today we have to use
--load
(or, rather, specifyoutputs: ["type=docker"]
because--load
isn't directly exposed via the DockerImageAsset construct) for CDK to then tag and push it to the ECR repository. As mentioned above, this is slow and unnecessary when using BuildKit.Use Case
Since the introduction of the App Staging Synthesizer alpha package, I've been migrating my Docker Images to be managed by CDK (previously, I didn't do this because of the messy singleton CDK ECR repository).
As part of this process, I wanted to use the
cacheTo
/cacheFrom
properties on theDockerImageAsset
in my GitHub Actions workflow to build these images as quickly as possible. The GitHub Actions (gha
) caching backend is only available when using Docker BuildKit.However, because of the three-step (
build
,tag
,push
) pattern used by CDK, I have to also includeoutputs: ["type=docker"]
to export the image from the BuildKit container to the docker daemon. Otherwise, I get an error message that looks like this:The reason for this error is because, by default, Docker BuildKit does not export the image to the docker image container. We can work around this issue by using
--load
(oroutputs: ["type=docker"]
) but, this adds additional, unnecessary time to the build process.Proposed Solution
cdk-assets
should be updated to dynamically detect that it's running in a Docker BuildKit builder. When it's running in a BuildKit container, the--tag
parameter to thedocker build
command should be the final tag (e.g.,12345678910.dkr.ecr.us-east-2.amazonaws.com/myapp/http_server:sha
, not justhttp_server:sha
) and the--push
flag should be set.Then, with BuildKit, it's a single command (
build --push
) instead of three (build
,tag
,push
).Other Information
No response
Acknowledgements
CDK version used
2.136.0 (build 94fd33b)
Environment details (OS name and version, etc.)
MacOS Sonoma
The text was updated successfully, but these errors were encountered: