Skip to content

Comments

feat(cdk-assets-lib): support --build-context flag for docker image builds#1128

Open
cogwirrel wants to merge 3 commits intoaws:mainfrom
cogwirrel:feat/docker-build-context
Open

feat(cdk-assets-lib): support --build-context flag for docker image builds#1128
cogwirrel wants to merge 3 commits intoaws:mainfrom
cogwirrel:feat/docker-build-context

Conversation

@cogwirrel
Copy link
Member

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.

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

…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-commenter
Copy link

codecov-commenter commented Feb 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.72%. Comparing base (6b190a2) to head (1e24824).

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           
Flag Coverage Δ
suite.unit 87.72% <ø> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@iankhou
Copy link
Contributor

iankhou commented Feb 17, 2026

Hi @cogwirrel, thanks for the contribution! Can you add some details (logs, etc.) as to how you tested/verified this change?

Copy link
Contributor

@iankhou iankhou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see comment on the PR, requesting verification of fix.

@cogwirrel
Copy link
Member Author

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:

cd /path/to/aws-cdk-cli
yarn build

cd packages/@aws-cdk/integ-runner
yarn link

cd /path/to/aws-cdk
yarn link @aws-cdk/integ-runner

cd packages/@aws-cdk-testing/framework-integ
yarn integ test/aws-ecr-assets/test/integ.assets-docker-build-context.js --update-on-failed --no-clean

Output:

$ yarn integ test/aws-ecr-assets/test/integ.assets-docker-build-context.js --update-on-failed --no-clean
yarn run v1.22.22
$ integ-runner --unstable=toolkit-lib-engine --language javascript test/aws-ecr-assets/test/integ.assets-docker-build-context.js --update-on-failed --no-clean
[Removed] The toolkit-lib engine is now the default engine. This flag can be safely removed. You may choose to temporarily revert to the old engine by adding the `--unstable=deprecated-cli-engine` option.
The toolkit-lib engine is now the default and only engine. This flag can be safely removed.

Verifying integration test snapshots...

  NEW        aws-ecr-assets/test/integ.assets-docker-build-context 0s

Snapshot Results: 

Tests:    1 failed, 1 total
Failed: /Users/jacsteve/test/aws-cdk/packages/@aws-cdk-testing/framework-integ/test/aws-ecr-assets/test/integ.assets-docker-build-context.js

Running integration tests for failed tests...

Running in parallel across regions: us-east-1, us-east-2, us-west-2
Running test /Users/jacsteve/test/aws-cdk/packages/@aws-cdk-testing/framework-integ/test/aws-ecr-assets/test/integ.assets-docker-build-context.js in us-east-1
#0 building with "desktop-linux" instance using docker driver

#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 202B done
#1 DONE 0.0s

#2 [context mycontext] load .dockerignore
#2 transferring mycontext: 2B done
#2 DONE 0.0s

#3 [internal] load metadata for public.ecr.aws/docker/library/python:3.12-slim
#3 DONE 1.4s

#4 [internal] load .dockerignore
#4 transferring context: 2B done
#4 DONE 0.0s

#5 [context mycontext] load from client
#5 transferring mycontext: 62B done
#5 DONE 0.0s

#6 [stage-0 1/4] FROM public.ecr.aws/docker/library/python:3.12-slim@sha256:9e01bf1ae5db7649a236da7be1e94ffbbbdd7a93f867dd0d8d5720d9e1f89fab
#6 resolve public.ecr.aws/docker/library/python:3.12-slim@sha256:9e01bf1ae5db7649a236da7be1e94ffbbbdd7a93f867dd0d8d5720d9e1f89fab done
#6 CACHED

#7 [stage-0 2/4] WORKDIR /src
#7 DONE 0.0s

#8 [internal] load build context
#8 transferring context: 1.47kB done
#8 DONE 0.0s

#9 [stage-0 3/4] ADD . /src
#9 DONE 0.0s

#10 [stage-0 4/4] COPY --from=mycontext hello.txt /src/hello.txt
#10 DONE 0.0s

#11 exporting to image
#11 exporting layers 0.0s done
#11 exporting manifest sha256:992a5adc688540065441c891780940e0ced2fb80c52d2506528b9be24e468efe done
#11 exporting config sha256:3868dfb27c225e1e9721e779cf14f0f7376154f8991fa1aaa668f4622acdb089
#11 exporting config sha256:3868dfb27c225e1e9721e779cf14f0f7376154f8991fa1aaa668f4622acdb089 done
#11 naming to docker.io/library/cdkasset-21592aa0c60f855735949fa2ddd50ccfe5c2662eea8c60a4c95742dbc5aa3206:latest done
#11 unpacking to docker.io/library/cdkasset-21592aa0c60f855735949fa2ddd50ccfe5c2662eea8c60a4c95742dbc5aa3206:latest 0.0s done
#11 DONE 0.1s
The push refers to repository [966839982822.dkr.ecr.us-east-1.amazonaws.com/cdk-hnb659fds-container-assets-966839982822-us-east-1]
09fa645f2e8f: Waiting
0a8d67d1dd0e: Waiting
78920ca4cc09: Waiting
bfa636a0362e: Waiting
acb1cdc2efd2: Waiting
93470b47ec3c: Waiting
3ea009573b47: Waiting
acb1cdc2efd2: Waiting
93470b47ec3c: Waiting
3ea009573b47: Waiting
09fa645f2e8f: Waiting
0a8d67d1dd0e: Waiting
78920ca4cc09: Waiting
bfa636a0362e: Waiting
bfa636a0362e: Waiting
acb1cdc2efd2: Waiting
93470b47ec3c: Waiting
3ea009573b47: Waiting
09fa645f2e8f: Waiting
0a8d67d1dd0e: Waiting
78920ca4cc09: Waiting
09fa645f2e8f: Waiting
0a8d67d1dd0e: Waiting
78920ca4cc09: Waiting
bfa636a0362e: Waiting
acb1cdc2efd2: Waiting
93470b47ec3c: Waiting
3ea009573b47: Waiting
93470b47ec3c: Waiting
3ea009573b47: Waiting
09fa645f2e8f: Waiting
0a8d67d1dd0e: Waiting
78920ca4cc09: Waiting
bfa636a0362e: Waiting
acb1cdc2efd2: Waiting
acb1cdc2efd2: Waiting
93470b47ec3c: Waiting
3ea009573b47: Waiting
09fa645f2e8f: Waiting
0a8d67d1dd0e: Waiting
78920ca4cc09: Waiting
bfa636a0362e: Waiting
09fa645f2e8f: Waiting
0a8d67d1dd0e: Waiting
78920ca4cc09: Waiting
bfa636a0362e: Waiting
acb1cdc2efd2: Waiting
93470b47ec3c: Waiting
3ea009573b47: Waiting
0a8d67d1dd0e: Waiting
78920ca4cc09: Waiting
bfa636a0362e: Waiting
acb1cdc2efd2: Waiting
93470b47ec3c: Waiting
3ea009573b47: Waiting
09fa645f2e8f: Waiting
bfa636a0362e: Waiting
acb1cdc2efd2: Waiting
93470b47ec3c: Waiting
3ea009573b47: Waiting
09fa645f2e8f: Waiting
0a8d67d1dd0e: Waiting
78920ca4cc09: Waiting
acb1cdc2efd2: Waiting
93470b47ec3c: Waiting
3ea009573b47: Waiting
09fa645f2e8f: Waiting
0a8d67d1dd0e: Waiting
78920ca4cc09: Waiting
bfa636a0362e: Waiting
bfa636a0362e: Waiting
acb1cdc2efd2: Waiting
93470b47ec3c: Waiting
3ea009573b47: Waiting
09fa645f2e8f: Waiting
0a8d67d1dd0e: Waiting
78920ca4cc09: Waiting
3ea009573b47: Waiting
09fa645f2e8f: Waiting
0a8d67d1dd0e: Waiting
78920ca4cc09: Waiting
bfa636a0362e: Waiting
acb1cdc2efd2: Waiting
93470b47ec3c: Waiting
acb1cdc2efd2: Waiting
93470b47ec3c: Waiting
3ea009573b47: Waiting
09fa645f2e8f: Waiting
0a8d67d1dd0e: Waiting
78920ca4cc09: Waiting
bfa636a0362e: Waiting
93470b47ec3c: Waiting
3ea009573b47: Waiting
09fa645f2e8f: Waiting
0a8d67d1dd0e: Waiting
78920ca4cc09: Waiting
bfa636a0362e: Waiting
acb1cdc2efd2: Waiting
09fa645f2e8f: Waiting
0a8d67d1dd0e: Waiting
78920ca4cc09: Waiting
bfa636a0362e: Waiting
acb1cdc2efd2: Waiting
93470b47ec3c: Waiting
3ea009573b47: Waiting
93470b47ec3c: Waiting
09fa645f2e8f: Waiting
78920ca4cc09: Waiting
acb1cdc2efd2: Waiting
09fa645f2e8f: Waiting
78920ca4cc09: Waiting
93470b47ec3c: Waiting
78920ca4cc09: Waiting
93470b47ec3c: Waiting
78920ca4cc09: Waiting
78920ca4cc09: Waiting
78920ca4cc09: Waiting
0a8d67d1dd0e: Pushed
93470b47ec3c: Pushed
09fa645f2e8f: Pushed
78920ca4cc09: Pushed
bfa636a0362e: Pushed
acb1cdc2efd2: Pushed
3ea009573b47: Pushed
21592aa0c60f855735949fa2ddd50ccfe5c2662eea8c60a4c95742dbc5aa3206: digest: sha256:992a5adc688540065441c891780940e0ced2fb80c52d2506528b9be24e468efe size: 1676
  SUCCESS    aws-ecr-assets/test/integ.assets-docker-build-context-DockerBuildContextTest/DefaultTest 66.831s
      

Test Results: 

Tests:    1 passed, 1 total
Not cleaning up stacks since "--no-clean" was used
✨  Done in 68.66s.

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:

Screenshot 2026-02-18 at 6 50 43 am

(I've since torn the service down)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants