feat(cdk-assets-lib): support --build-context flag for docker image builds#1128
feat(cdk-assets-lib): support --build-context flag for docker image builds#1128
Conversation
…uilds Add support for `dockerBuildContexts` in the cloud assembly schema and docker build logic. This allows passing additional named build contexts to the `docker build` command via the `--build-context` flag, enabling multi-context Docker builds where files can be sourced from directories, URLs, or other Docker images outside the primary build directory.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1128 +/- ##
=======================================
Coverage 87.71% 87.72%
=======================================
Files 72 72
Lines 10129 10129
Branches 1338 1338
=======================================
+ Hits 8885 8886 +1
+ Misses 1218 1217 -1
Partials 26 26
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Hi @cogwirrel, thanks for the contribution! Can you add some details (logs, etc.) as to how you tested/verified this change? |
iankhou
left a comment
There was a problem hiding this comment.
Please see comment on the PR, requesting verification of fix.
|
Hi @iankhou! Thanks for taking a look! I tested in conjunction with my PR to aws-cdk-lib here: aws/aws-cdk#36930 In that PR I added an integration test, and verified that it worked end to end with a linked copy of aws-cdk-cli. The key parts of the integration test were declaring the build context: const asset = new assets.DockerImageAsset(stack, 'DockerImageWithBuildContext', {
directory: path.join(__dirname, 'demo-image-build-context', 'image'),
buildContexts: {
mycontext: path.join(__dirname, 'demo-image-build-context', 'context'),
},
});And then using it in the Dockerfile: FROM public.ecr.aws/docker/library/python:3.12-slim
EXPOSE 8000
WORKDIR /src
ADD . /src
COPY --from=mycontext hello.txt /src/hello.txt
CMD ["python3", "index.py"]To deploy, my steps were roughly: Output: For a final end to end test, I created an ECS "Express Service" pointing at the image deployed by the integ test, and everything deployed successfully:
(I've since torn the service down) |

Add support for
dockerBuildContextsin the cloud assembly schema and docker build logic. This allows passing additional named build contexts to thedocker buildcommand via the--build-contextflag, enabling multi-context Docker builds where files can be sourced from directories, URLs, or other Docker images outside the primary build directory.This PR adds the CLI changes required to address aws/aws-cdk#31598
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license