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.75% 87.78% +0.02%
==========================================
Files 72 72
Lines 10137 10137
Branches 1339 1339
==========================================
+ Hits 8896 8899 +3
+ Misses 1216 1213 -3
Partials 25 25
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 @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