|
| 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\. |
0 commit comments