Skip to content

ecr-assets: unable to use one Dockerfile to build multiple images #5683

@iliapolo

Description

@iliapolo

Creating two DockerImageAssets, using a different target in each of them, will result in only one of the images actually built and pushed. As well as only one ECR repository created.

Similarly, this will also happen when specifying different build-args in each asset definition.

Reproduction Steps

Consider the following stack definition:

export class AwsCdkPlaygroundStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    new ecr.DockerImageAsset(this, 'Image1', {
      directory: path.join(__dirname, 'image'),
      target: "builder"
    })

    new ecr.DockerImageAsset(this, 'Image2', {
      directory: path.join(__dirname, 'image'),
      target: "runner",
    })

  }
}

Just for context, the (degenerate) Dockerfile looks something like this:

FROM golang:1.7.3 AS builder
FROM alpine:latest AS runner 

This setup should result in two images, one for the builder target and one for the runner.

Error Log

Its not actually causing any errors, but you can see in the output that it only pushed one image to a single repository.

AwsCdkPlaygroundStack: deploying...
Sending build context to Docker daemon  2.048kB
Step 1/1 : FROM golang:1.7.3 AS builder
 ---> ef15416724f6
Successfully built ef15416724f6
Successfully tagged 185706627232.dkr.ecr.us-east-2.amazonaws.com/cdk/awscdkplaygroundstackimage19f2089d4:latest
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Login Succeeded
 ⌛ Pushing Docker image for cdk.out/asset.1f6cca87ce602dc2fe4066751259f3ebdffea55b31e70d95051694bd0b9f048a; this may take a while.
The push refers to repository [185706627232.dkr.ecr.us-east-2.amazonaws.com/cdk/awscdkplaygroundstackimage19f2089d4]
2d9a008a6ee1: Preparing
a3e878ca1a1c: Preparing
4588c2179cdc: Preparing
9168b67dbc50: Preparing
9f17712cba0b: Preparing
223c0d04a137: Preparing
fe4c16cbf7a4: Preparing
223c0d04a137: Waiting
fe4c16cbf7a4: Waiting
2d9a008a6ee1: Pushed
a3e878ca1a1c: Pushed
223c0d04a137: Pushed
9f17712cba0b: Pushed
9168b67dbc50: Pushed
fe4c16cbf7a4: Pushed
4588c2179cdc: Pushed
latest: digest: sha256:37d263ccd240e113a752c46306ad004e36532ce118eb3131d9f76f43cc606d5d size: 1792
185706627232.dkr.ecr.us-east-2.amazonaws.com/cdk/awscdkplaygroundstackimage19f2089d4@sha256:37d263ccd240e113a752c46306ad004e36532ce118eb3131d9f76f43cc606d5d|golang@sha256:340212e9c5d062f3bfe58ff02768da70234ea734bd022a357ee6be2a6d963505|
AwsCdkPlaygroundStack: creating CloudFormation changeset..

Environment

  • CLI Version : 1.19.0 (build 5597bbe)
  • Framework Version: 1.19.0 (build 5597bbe)
  • OS : OSX
  • Language : Typescript

Other

This also affects use cases where one has multiple Dockerfiles and uses the file property to specify different files for different images.

Root Cause

Since the asset directory is the same in both assets, the resulting hash is the same as well, which causes a false deduplication.

Proposed Solution

The Hash of the asset should consider the command used to actually build the container. This way, passing a different target/build-arg/file will result in a new hash, and everything else should follow.

Funnily enough, we already thought of this a while back.

Other affected modules?

This problem isn't necessarily tied to docker, it will happen in every asset that can produce multiple artifacts from the same directory content. Can we think of something else that might be affected?


This is 🐛 Bug Report

Metadata

Metadata

Assignees

Labels

@aws-cdk/assetsRelated to the @aws-cdk/assets package@aws-cdk/aws-ecrRelated to Amazon Elastic Container RegistrybugThis issue is a bug.needs-triageThis issue or PR still needs to be triaged.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions