Skip to content

Commit c0849ea

Browse files
authored
feat(ecr-assets): add support for docker build context (#36930)
### Issue # (if applicable) Fixes #31598 ### Reason for this change Add support for docker's `--build-context` flag in docker builds. This is useful for a few reasons outlined in the linked issue (and other similar issues), such as: - Sharing files from directories outside the Docker build directory - Using specific image versions as build contexts (`docker-image://alpine:latest`) - Referencing remote URLs as build contexts ### Description of changes Adds support for Docker's `--build-context` flag when building Docker image assets. This allows users to specify additional named build contexts that can be referenced in Dockerfiles via `COPY --from=<name>`. - Added `buildContexts` (optional `Record<string, string>`) to `DockerBuildOptions`, `DockerImageAssetOptions`, `DockerImageAssetInvalidationOptions`, and `DockerImageAssetSource` - Updated `DockerImage.fromBuild()` to pass `--build-context key=value` flags to the docker build command - Wired `buildContexts` through the full asset pipeline: `DockerImageAsset` → synthesizer → asset manifest → cloud assembly schema - Added token validation for `buildContexts` keys and values (same as `buildArgs`) - Added `buildContexts` to asset hash invalidation (controllable via `invalidation.buildContexts`) - Added `ASSET_RESOURCE_METADATA_DOCKER_BUILD_CONTEXTS_KEY` metadata constant - Updated the `aws-ecr-assets` README with documentation and usage example In terms of design decisions, this follows the same pattern as `buildArgs`. The necessary changes to the CLI have been released: aws/aws-cdk-cli#1128 ### Describe any new or updated permissions being added N/A ### Description of how you validated changes Integration test ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent ac3eadc commit c0849ea

File tree

22 files changed

+1214
-0
lines changed

22 files changed

+1214
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello from build context!
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM public.ecr.aws/docker/library/python:3.12-slim
2+
EXPOSE 8000
3+
WORKDIR /src
4+
ADD . /src
5+
COPY --from=mycontext hello.txt /src/hello.txt
6+
CMD ["python3", "index.py"]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/python
2+
import sys
3+
import textwrap
4+
import http.server
5+
import socketserver
6+
7+
PORT = 8000
8+
9+
# Read the file that was copied from the build context
10+
try:
11+
with open('/src/hello.txt', 'r') as f:
12+
context_message = f.read().strip()
13+
except FileNotFoundError:
14+
context_message = 'ERROR: hello.txt not found - build context may not have worked'
15+
16+
17+
class Handler(http.server.SimpleHTTPRequestHandler):
18+
def do_GET(self):
19+
self.send_response(200)
20+
self.send_header('Content-Type', 'text/html')
21+
self.end_headers()
22+
self.wfile.write(textwrap.dedent('''\
23+
<!doctype html>
24+
<html><head><title>It works</title></head>
25+
<body>
26+
<h1>Hello from the integ test container with build context</h1>
27+
<p>Message from build context: {message}</p>
28+
<img src="https://media.giphy.com/media/nFjDu1LjEADh6/giphy.gif">
29+
</body>
30+
''').format(message=context_message).encode('utf-8'))
31+
32+
33+
def main():
34+
httpd = http.server.HTTPServer(("", PORT), Handler)
35+
print("serving at port", PORT)
36+
print("message from build context:", context_message)
37+
httpd.serve_forever()
38+
39+
40+
if __name__ == '__main__':
41+
main()

packages/@aws-cdk-testing/framework-integ/test/aws-ecr-assets/test/integ.assets-docker-build-context.js.snapshot/DockerBuildContextTestDefaultTestDeployAssert7E73C929.assets.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ecr-assets/test/integ.assets-docker-build-context.js.snapshot/DockerBuildContextTestDefaultTestDeployAssert7E73C929.template.json

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ecr-assets/test/integ.assets-docker-build-context.js.snapshot/asset.21592aa0c60f855735949fa2ddd50ccfe5c2662eea8c60a4c95742dbc5aa3206/Dockerfile

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ecr-assets/test/integ.assets-docker-build-context.js.snapshot/asset.21592aa0c60f855735949fa2ddd50ccfe5c2662eea8c60a4c95742dbc5aa3206/index.py

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ecr-assets/test/integ.assets-docker-build-context.js.snapshot/cdk.out

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ecr-assets/test/integ.assets-docker-build-context.js.snapshot/integ-assets-docker-build-context.assets.json

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{
2+
"Resources": {
3+
"MyUserDC45028B": {
4+
"Type": "AWS::IAM::User"
5+
},
6+
"MyUserDefaultPolicy7B897426": {
7+
"Type": "AWS::IAM::Policy",
8+
"Properties": {
9+
"PolicyDocument": {
10+
"Statement": [
11+
{
12+
"Action": [
13+
"ecr:BatchCheckLayerAvailability",
14+
"ecr:BatchGetImage",
15+
"ecr:GetDownloadUrlForLayer"
16+
],
17+
"Effect": "Allow",
18+
"Resource": {
19+
"Fn::Join": [
20+
"",
21+
[
22+
"arn:",
23+
{
24+
"Ref": "AWS::Partition"
25+
},
26+
":ecr:",
27+
{
28+
"Ref": "AWS::Region"
29+
},
30+
":",
31+
{
32+
"Ref": "AWS::AccountId"
33+
},
34+
":repository/",
35+
{
36+
"Fn::Sub": "cdk-hnb659fds-container-assets-${AWS::AccountId}-${AWS::Region}"
37+
}
38+
]
39+
]
40+
}
41+
},
42+
{
43+
"Action": "ecr:GetAuthorizationToken",
44+
"Effect": "Allow",
45+
"Resource": "*"
46+
}
47+
],
48+
"Version": "2012-10-17"
49+
},
50+
"PolicyName": "MyUserDefaultPolicy7B897426",
51+
"Users": [
52+
{
53+
"Ref": "MyUserDC45028B"
54+
}
55+
]
56+
}
57+
}
58+
},
59+
"Outputs": {
60+
"ImageUri": {
61+
"Value": {
62+
"Fn::Sub": "${AWS::AccountId}.dkr.ecr.${AWS::Region}.${AWS::URLSuffix}/cdk-hnb659fds-container-assets-${AWS::AccountId}-${AWS::Region}:21592aa0c60f855735949fa2ddd50ccfe5c2662eea8c60a4c95742dbc5aa3206"
63+
}
64+
}
65+
},
66+
"Parameters": {
67+
"BootstrapVersion": {
68+
"Type": "AWS::SSM::Parameter::Value<String>",
69+
"Default": "/cdk-bootstrap/hnb659fds/version",
70+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
71+
}
72+
},
73+
"Rules": {
74+
"CheckBootstrapVersion": {
75+
"Assertions": [
76+
{
77+
"Assert": {
78+
"Fn::Not": [
79+
{
80+
"Fn::Contains": [
81+
[
82+
"1",
83+
"2",
84+
"3",
85+
"4",
86+
"5"
87+
],
88+
{
89+
"Ref": "BootstrapVersion"
90+
}
91+
]
92+
}
93+
]
94+
},
95+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
96+
}
97+
]
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)