AWS Codebuild Dotnet Core Lambda Docker COPY failed: stat /var/lib/docker/tmp/docker-builder no such file or directory

I have Dotnet Core AWS Lambda application created using dotnet new -i Amazon.Lambda.Templates and dotnet new serverless.AspNetCoreWebAPI, I am using a custom docker container image for this Lambda function.

I can deploy the Lambda function using the dotnet lambda deploy-serverless command, however I want to build the image and push the image in AWS Codebuild. For this I wrote a buildspec.yml file, like this.

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

And I have the Dockerfile.codebuild file like this

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

The ${CODEBUILD_SRC_DIR} ‘s value seems to be like /codebuild/output/srcXXXXXX/src/git-codecommit.xx-xxxxx-1.amazonaws.com/v1/repos/myrepo/bin/Release/net5.0/linux-x64/publish

However, the line COPY ${CODEBUILD_SRC_DIR} . in the Dockerfile.codebuild is showing error like this.

COPY failed: 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: no such file or directory

How can I avoid the /var/lib/docker/tmp/docker-builderXXXXXXXX from the path and just have the codebuild/output/srcXXXXXX/src/git-codecommit.xx-xxxxx-1.amazonaws.com/v1/repos/myrepo/bin/Release/net5.0/linux-x64/publish path so that I can copy the folder ?

Source: Docker Questions

LEAVE A COMMENT