I'm using CDK to deploy an application that uses containers defined in a docker-compose file. I can see that there are ways to create image assets from Dockerfiles, such as the DockerImageAsset class or the ContainerImage.fromAsset method. But I can't find any resources for working with services defined in a docker-compose.yml file. I could build the images locally and push to ECR, but it would be nice to have this process automated. What is the best way to handle this?
I have not used or seen docker-compose used with CDK, but CDK would be for defining the infrastructure on AWS and docker-compose for running the stack locally I imagine, not using compose with CDK.
You reference a Dockerfile in your CDK setup and CDK builds it for you and pushes it to ECR. The ECR repo is set up by CDK when running ``. So that handles that part for you behind the scenes. Quite convenient!
When you define CDK code that references a Dockerfile, you then pass the referenced image of the created ECR image to a task definition, for example, that then uses the image in AWS when deployed. Same can be done using lambdas also.
I hope this example below clears it up a bit. You could then for example use a pipeline like github actions or circleci to create a job that runs CDK deploy
for you.
// This pushes it to ECR after building the file
const dockerImage = new DockerImageAsset(this, 'some-image', {
directory: path.join(__dirname, '../../')
})
this.loadBalancer = new ApplicationLoadBalancedFargateService(this, `${namePrefix}-lb`, {
cluster,
serviceName: `${namePrefix}-lb`,
taskImageOptions: {
containerName: `${namePrefix}-task`,
image: ecs.ContainerImage.fromDockerImageAsset(dockerImage), <-- this is where you pass the docker image reference
environment: containerEnvs,
containerPort: parseInt(containerEnvs.PORT),
enableLogging: true,
taskRole: role,
},
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With