首页 > 解决方案 > AWS Codebuild Dotnet Core Lambda Docker COPY 失败:stat /var/lib/docker/tmp/docker-builder 没有这样的文件或目录

问题描述

我使用 dotnet new -i Amazon.Lambda.Templates 和 dotnet new serverless.AspNetCoreWebAPI 创建了 Dotnet Core AWS Lambda 应用程序,我正在为此 Lambda 函数使用自定义 docker 容器映像。

我可以使用 dotnet lambda deploy-serverless 命令部署 Lambda 函数,但是我想构建映像并将映像推送到 AWS Codebuild。为此,我编写了一个 buildspec.yml 文件,如下所示。

version: 0.2

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...     
      - wget https://packages.microsoft.com/config/ubuntu/21.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
      - dpkg -i packages-microsoft-prod.deb
      - rm packages-microsoft-prod.deb
      - apt-get update
      - apt-get install -y apt-transport-https && apt-get update && apt-get install -y dotnet-sdk-5.0
      - dotnet build -c Release -r linux-x64
      - OutputDirectory=`echo /bin/Release/net5.0/linux-x64/publish`
      - docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG -f Dockerfile.codebuild --build-arg CODEBUILD_SRC_DIR=$CODEBUILD_SRC_DIR$OutputDirectory .
      - docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG      
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker image...
      - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG

我有这样的 Dockerfile.codebuild 文件

FROM public.ecr.aws/lambda/dotnet:5.0
ARG CODEBUILD_SRC_DIR=defaultValue
WORKDIR /var/task

# This COPY command copies the .NET Lambda project's build artifacts from the host machine into the image. 
# The source of the COPY should match where the .NET Lambda project publishes its build artifacts. If the Lambda function is being built 
# with the AWS .NET Lambda Tooling, the `--docker-host-build-output-dir` switch controls where the .NET Lambda project
# will be built. The .NET Lambda project templates default to having `--docker-host-build-output-dir`
# set in the aws-lambda-tools-defaults.json file to "bin/Release/net5.0/linux-x64/publish".
#
# Alternatively Docker multi-stage build could be used to build the .NET Lambda project inside the image.
# For more information on this approach checkout the project's README.md file.
RUN echo "CODEBUILD_SRC_DIR value"
RUN echo ${CODEBUILD_SRC_DIR}
RUN echo "CODEBUILD_SRC_DIR value-- end"
COPY ${CODEBUILD_SRC_DIR} .

RUN yum install -y amazon-linux-extras 
RUN amazon-linux-extras install epel -y
RUN yum install -y libgdiplus

${CODEBUILD_SRC_DIR} 的值似乎类似于 /codebuild/output/srcXXXXXX/src/git-codecommit.xx-xxxxx-1.amazonaws.com/v1/repos/myrepo/bin/Release/net5.0/linux -x64/发布

但是,行 COPY ${CODEBUILD_SRC_DIR} 。在 Dockerfile.codebuild 中显示这样的错误。

复制失败:stat /var/lib/docker/tmp/docker-builderXXXXXXXX/codebuild/output/srcXXXXXX/src/git-codecommit.xx-xxxxx-1.amazonaws.com/v1/repos/myrepo/bin/Release/net5 .0/linux-x64/publish: 没有这样的文件或目录

我怎样才能避免/var/lib/docker/tmp/docker-builderXXXXXXXX路径中的路径并且只有codebuild/output/srcXXXXXX/src/git-codecommit.xx-xxxxx-1.amazonaws.com/v1/repos/myrepo/bin/Release/net5.0/linux-x64/publish路径以便我可以复制文件夹?

标签: amazon-web-servicesdockeraws-codebuildaws-lambda-containers

解决方案


推荐阅读