如何解决使用CDK上传图片到ECR失败的问题?

How do I resolve the problem of image uploads to ECR using CDK failing?

背景

我们正在尝试从指定目录中的 Docker 文件创建一个 Docker 图像,将其推送到 cdk 的默认存储库,将图像复制到新创建的存储库,然后调用它来自 lambda 函数。

导致问题的 ecr.ts 文件的目录结构和内容如下。

root
  └ services
      ├ download_pdf
      │   ├ lambda
      :   │   └ Dockerfile
          └ infra
              ├ cdk.json
              ├ package.json
              :
              ├ dist
              └ src
                 ├ index.ts
                 ├ resource
                 │   ├ api-gateway.ts
                 │   ├ lambda.ts
                 │   └ ecr.ts
                 └ stack
                     └ download-pdf-stack.ts              
import {createResourceName} from '@packages/infra-lib';
import {RemovalPolicy} from 'aws-cdk-lib';
import {Repository} from 'aws-cdk-lib/aws-ecr';
import {DockerImageAsset} from 'aws-cdk-lib/aws-ecr-assets';
import {DockerImageName, ECRDeployment} from 'cdk-ecr-deployment';
import {Construct} from 'constructs';
import path from 'path';

export function createRepositoryPuppeteer(scope: Construct): Repository {
  const repo = new Repository(scope, 'RepositoryForPuppeteer', {
    repositoryName: createResourceName(scope, 'repository-puppeteer'),
    removalPolicy: RemovalPolicy.DESTROY,
  });
  const image = new DockerImageAsset(scope, 'DockerImageAssetPuppeteer', {
    directory: path.join(__dirname, '../../../lambda'),
  });
  new ECRDeployment(scope, 'ECRDeploymentPuppeteer', {
    src: new DockerImageName(image.imageUri),
    dest: new DockerImageName(`${repo.repositoryUri}:latest`),
  });

  return repo;
}

问题

当我们尝试使用上述方法进行部署时,出现以下错误。

$ yarn workspace @download-pdf/infra cdk deploy -vv DownloadPdfStack

[50%] check: Check xxxxxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/cdk-yyyyyyyyy-container-assets-xxxxxxxxxxxx-ap-northeast-1:zzzzzzzzzzzzzzzzzzzzzzzz
[AWS ecr 400 0.116s 0 retries] describeImages({
  repositoryName: 'cdk-hnb659fds-container-assets-xxxxxxxxxxxx-ap-northeast-1',
  imageIds: [
    {
      imageTag: 'zzzzzzzzzzzzzzzzzzzzzzzz'
    },
    [length]: 1
  ]
})
Call failed: describeImages({"repositoryName":"cdk-yyyyyyyyy-container-assets-xxxxxxxxxxxx-ap-northeast-1","imageIds":[{"imageTag":"zzzzzzzzzzzzzzzzzzzzzzzz"}]}) => The image with imageId {imageDigest:'null', imageTag:'zzzzzzzzzzzzzzzzzzzzzzzz'} does not exist within the repository with name 'cdk-yyyyyyyyy-container-assets-xxxxxxxxxxxx-ap-northeast-1' in the registry with id 'xxxxxxxxxxxx' (code=ImageNotFoundException)
[AWS ecr 200 0.099s 0 retries] getAuthorizationToken({})
[50%] debug: docker login --username AWS --password-stdin https://xxxxxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com
Error: write EPIPE
    at afterWriteDispatched (internal/stream_base_commons.js:156:25)
    at writeGeneric (internal/stream_base_commons.js:147:3)
    at Socket._writeGeneric (net.js:798:11)
    at Socket._write (net.js:810:8)
    at writeOrBuffer (internal/streams/writable.js:358:12)
    at Socket.Writable.write (internal/streams/writable.js:303:10)
    at /app/node_modules/cdk-assets/lib/private/shell.ts:28:19
    at new Promise (<anonymous>)
    at Object.shell (/app/node_modules/cdk-assets/lib/private/shell.ts:26:10)
    at Docker.execute (/app/node_modules/cdk-assets/lib/private/docker.ts:131:13)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed.
Exit code: 1

我们尝试把它注释掉,好像错误出现在'new DockerImageAsset'这句,但不知道错误的原因。我们想知道出现上述问题的原因以及解决方法。

我找到了这个问题的原因:我在 docker 容器中使用 cdk,但是由于 docker 没有安装在这个容器中,所以我无法从Dockerfile.