Skip to content

Commit d2211c5

Browse files
committed
Update: Add documentation on building and deploying container image assets
1 parent 3b5d796 commit d2211c5

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

v2/build-containers.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Build and deploy container image assets in CDK apps<a name="build-containers"></a>
2+
3+
When you build container image assets with the AWS Cloud Development Kit \(AWS CDK\), Docker is utilized by default to perform these actions\. If you want to use a different container management tool, you can replace Docker through the `CDK_DOCKER` environment variable\.
4+
5+
## Example: Build and publish a container image asset with the AWS CDK<a name="build-containers-intro-example"></a>
6+
7+
The following is a simple example of an AWS CDK app that builds and publishes a container asset to Amazon Elastic Container Registry \(Amazon ECR\) using Docker by default:
8+
9+
**Project structure**:
10+
11+
```
12+
my-cdk-app/
13+
├── lib/
14+
│ ├── my-stack.ts
15+
│ └── docker/
16+
│ ├── Dockerfile
17+
│ └── app/
18+
│ └── index.js
19+
├── bin/
20+
│ └── my-cdk-app.ts
21+
├── package.json
22+
├── tsconfig.json
23+
└── cdk.json
24+
```
25+
26+
**Dockerfile:**
27+
28+
```
29+
FROM public.ecr.aws/lambda/nodejs:16
30+
31+
# Copy application code
32+
COPY app/ /var/task/
33+
34+
# (Optional) Install dependencies
35+
# RUN npm install
36+
37+
# The AWS Lambda Node.js base image looks for index.handler by default
38+
```
39+
40+
**Application code**:
41+
42+
In `lib/docker/app/index.js`:
43+
44+
```
45+
console.log("Hello from inside the container!");
46+
```
47+
48+
**CDK stack**:
49+
50+
```
51+
import * as cdk from 'aws-cdk-lib';
52+
import { Construct } from 'constructs';
53+
import * as ecr_assets from 'aws-cdk-lib/aws-ecr-assets';
54+
55+
export class MyStack extends cdk.Stack {
56+
constructor(scope: Construct, id: string) {
57+
super(scope, id);
58+
59+
// Define a Docker image asset
60+
const dockerImageAsset = new ecr_assets.DockerImageAsset(this, 'MyDockerImage', {
61+
directory: 'lib/docker', // Path to the directory containing the Dockerfile
62+
});
63+
64+
// Output the ECR URI
65+
new cdk.CfnOutput(this, 'ECRImageUri', {
66+
value: dockerImageAsset.imageUri,
67+
});
68+
}
69+
}
70+
```
71+
72+
**CDK app**:
73+
74+
```
75+
#!/usr/bin/env node
76+
import * as cdk from 'aws-cdk-lib';
77+
import { MyStack } from '../lib/my-stack';
78+
79+
const app = new cdk.App();
80+
new MyStack(app, 'MyStack');
81+
```
82+
83+
When we run `cdk deploy`, the AWS Cloud Development Kit \(AWS CDK\) Command Line Interface \(CLI\) does the following:
84+
85+
1. **Build the Docker image** – Run `docker build` locally based on the `Dockerfile` in the specified directory \(`lib/docker`\)\.
86+
87+
1. **Tag the image** – Run `docker tag` to tag the built image with a unique hash, based on the image contents\.
88+
89+
1. **Publish to Amazon ECR** – Run `docker push` to publish the container image to an Amazon ECR repository\. This repository must already exist\. It is created during the default bootstrapping process\.
90+
91+
1. **Output the Image URI** – After a successful deployment, the Amazon ECR URI of the published container image is output in your command prompt\. This is the URI of our Docker image in Amazon ECR\.
92+
93+
## How to replace Docker with another container management tool<a name="build-container-replace"></a>
94+
95+
Use the `CDK_DOCKER` environment variable to specify the path to the binary of your replacement container management tool\. The following is an example of replacing Docker with Finch:
96+
97+
```
98+
$ which finch
99+
/usr/local/bin/finch # Locate the path to the binary
100+
101+
$ export CDK_DOCKER='/usr/local/bin/finch' # Set the environment variable
102+
103+
$ cdk deploy # Deploy using the replacement
104+
```
105+
106+
Aliasing or linking is not supported\. To replace Docker, you must use the `CDK_DOCKER` environment variable\.
107+
108+
## Supported Docker drop\-in replacement engines<a name="build-container-supported"></a>
109+
110+
Finch is supported, although there may be some Docker features that are unavailable or may work differently as the tool evolves\. For more information on Finch, see [Ready for Flight: Announcing Finch 1\.0 GA\!](https://aws.amazon.com/blogs/opensource/ready-for-flight-announcing-finch-1-0-ga/) in the *AWS Open Source Blog*\.
111+
112+
Other container management tools may work\. The CDK doesn’t check which Docker replacement you are using to determine if it’s supported\. If the tool has equivalent Docker commands and behaves similarly, it should work\.

v2/doc-history.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The table below represents significant documentation milestones\. We fix errors
77

88
| Change | Description | Date |
99
| --- |--- |--- |
10+
| [Add documentation for building and deploying container image assets](#doc-history) | When you build container image assets with the AWS CDK, Docker is utilized by default to perform these actions\. If you want to use a different container management tool, you can replace Docker\. For more information, see [Build and deploy container image assets in CDK apps](https://docs.aws.amazon.com/cdk/v2/guide/build-containers.html)\. | March 21, 2025 |
1011
| [Add documentation for CDK Migrate feature](#doc-history) | Use the AWS CDK CLI `cdk migrate` command to migrate deployed AWS resources, deployed AWS CloudFormation stacks, and local AWS CloudFormation templates to AWS CDK\. For more information, see [Migrate to AWS CDK](https://docs.aws.amazon.com/cdk/v2/guide/migrate.html)\. | February 2, 2024 |
1112
| [IAM best practices updates](#doc-history) | Updated guide to align with the IAM best practices\. For more information, see [Security best practices in IAM](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)\. | March 23, 2023 |
1213
| [Document `cdk.json`](#doc-history) | Add documentation of `cdk.json` configuration values\. | April 20, 2022 |

v2/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ sponsored by Amazon.
7373
+ [Deploy AWS CDK applications](deploy.md)
7474
+ [AWS CDK policy validation at synthesis time](policy-validation-synthesis.md)
7575
+ [Continuous integration and delivery (CI/CD) using CDK Pipelines](cdk_pipeline.md)
76+
+ [Build and deploy container image assets in CDK apps](build-containers.md)
7677
+ [Troubleshoot AWS CDK deployments](deploy-troubleshoot.md)
7778
+ [Perform programmatic actions using the CDK Toolkit Library](toolkit-library.md)
7879
+ [Test AWS CDK applications](testing.md)

0 commit comments

Comments
 (0)