Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create Docker image assets in CDK from a docker-compose file?

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?

like image 801
James Kelleher Avatar asked Sep 17 '25 20:09

James Kelleher


1 Answers

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,
  },
})
like image 103
Frizzo Avatar answered Sep 19 '25 09:09

Frizzo