简体   繁体   English

如何从 docker-compose 文件在 CDK 中创建 Docker 图像资源?

[英]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.我正在使用 CDK 部署一个应用程序,该应用程序使用 docker-compose 文件中定义的容器。 I can see that there are ways to create image assets from Dockerfiles, such as the DockerImageAsset class or the ContainerImage.fromAsset method.我可以看到有一些方法可以从 Dockerfiles 创建图像资产,例如DockerImageAsset class 或ContainerImage.fromAsset方法。 But I can't find any resources for working with services defined in a docker-compose.yml file.但是我找不到任何资源来处理 docker-compose.yml 文件中定义的服务。 I could build the images locally and push to ECR, but it would be nice to have this process automated.我可以在本地构建图像并推送到 ECR,但如果这个过程自动化就好了。 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.我没有使用或看到 docker-compose 与 CDK 一起使用,但我想 CDK 将用于定义 AWS 上的基础设施,docker-compose 用于在本地运行堆栈,而不是将 compose 与 CDK 一起使用。

You reference a Dockerfile in your CDK setup and CDK builds it for you and pushes it to ECR.您在 CDK 设置中引用 Dockerfile,CDK 会为您构建它并将其推送到 ECR。 The ECR repo is set up by CDK when running ``. ECR 存储库在运行 `` 时由 CDK 设置。 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.当您定义引用 Dockerfile 的 CDK 代码时,您随后将创建的 ECR 映像的引用映像传递给任务定义,例如,然后在部署时在 AWS 中使用该映像。 Same can be done using lambdas also.同样可以使用 lambda 来完成。

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.例如,您可以使用 github 操作或 circleci 之类的管道来创建一个为您运行CDK deploy的作业。

// 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,
  },
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker 图片适用于 docker-compose up 但不适用于 Amazon ECS 或 Heroku - Docker Image works with docker-compose up but not on Amazon ECS or Heroku 在 Amazon EC2 上安装 docker-compose Linux 2. 9kb docker-compose 文件 - Installing docker-compose on Amazon EC2 Linux 2. 9kb docker-compose file docker-compose:如何在 docker.network 内外使用 minio - docker-compose: how to use minio in- and outside of the docker network 如何使用 docker-compose 将容器部署到谷歌云? - How to deploy container using docker-compose to google cloud? 带有 docker-compose 应用程序的 Google Jib,在将映像重建到 Docker 守护程序后重新启动应用程序的快速方法 - Google Jib with docker-compose application, Fast way to restart application after rebuild image to Docker daemon 如何使用 docker-compose 修复 dynamodb 本地调用 - How to fix dynamodb local call using docker-compose AWS CDK:如何使用 Typescript 创建 Docker Lambda 函数? - AWS CDK: How to create a Docker Lambda function with Typescript? 将 docker-compose 部署到 Cloud Run 或 GCP? - Deploying docker-compose to Cloud Run OR GCP? 使用 Rancher OS 运行 docker-compose - Running docker-compose with Rancher OS docker-compose 显示所有不是在 docker-compose.yml 中定义的服务 - docker-compose displaying all services not the one defined in docker-compose.yml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM