允许堆栈创建多个ecr图像

时间:2019-11-13 13:28:14

标签: aws-cdk

我有一个案例,单个Dockerfile能够构建共享目录的两个项目

cdk_project
src
  \Project1
  \Project2
  \ProjectShared
  \Dockerfile

使用CDK,我试图在堆栈中创建2个单独的服务

const web_app_image_asset = new DockerImageAsset(this, 'webapp', {

      directory: "../src",
      buildArgs: {
        "PROJECT_DIR": "webapp"
      }
    });

const api_image_asset = new DockerImageAsset(this, 'api', {
      directory: "../src",
      buildArgs: {
        "PROJECT_DIR": "api",
      }
    });

const web_service = new ecs_patterns.ApplicationLoadBalancedFargateService(
      cluster,
      "web-Service",
      {
        cluster,
        taskImageOptions: {
          image: ecs.ContainerImage.fromEcrRepository(web_app_image_asset .repository),
        },
      }
    );

const api_service = new ecs_patterns.ApplicationLoadBalancedFargateService(
      cluster,
      "api-Service",
      {
        cluster,
        taskImageOptions: {
          image: ecs.ContainerImage.fromEcrRepository(api_image_asset.repository),
        },
      }
    );

问题是,仅创建了一个映像,我期望一旦buildArg不同,它将允许docker构建第二个映像,但是不会。

Dockerfile

FROM abiosoft/caddy:php
ARG PROJECT_DIR=webapp

ADD PROJECT_SHARED /PROJECT_SHARED
RUN mkdir /app
WORKDIR /app
ARG PROJECT_DIR
COPY $PROJECT_DIR  .
COPY ./Caddyfile /etc/Caddyfile
RUN composer install 

1 个答案:

答案 0 :(得分:0)

我建议您在Dockerfile中使用目标。 最近,一个版本增加了在构建(https://github.com/aws/aws-cdk/pull/4185)中指定目标的可能性

然后您的Dockerfile可能看起来像这样:

FROM abiosoft/caddy:php as project_base

ADD PROJECT_SHARED /PROJECT_SHARED
RUN mkdir /app
WORKDIR /app

FROM project_base as project_webapp

ARG PROJECT_DIR=webapp
COPY $PROJECT_DIR  .
COPY ./Caddyfile /etc/Caddyfile
RUN composer install 

FROM project_base as project_api

ARG PROJECT_DIR=api
COPY $PROJECT_DIR  .
COPY ./Caddyfile /etc/Caddyfile
RUN composer install 

然后您的CDK代码如下:

const apiImage = new DockerImageAsset(this, 'apiImage', {
      directory: '../src',
      target: 'api',
    });

const webappImage = new DockerImageAsset(this, 'webappImage', {
      directory: '../src',
      target: 'webapp',
    });