How can I make Docker layer cache permanent when I build Docker images in CodeBuild

0

I'm using CodeBuild to build a Docker image for ECR. I want to leverage Docker layer cache to reduce build time, so I configured my CodeBuild project to use Docker layer cache.

Here's my CodeBuild project configuration written in CloudFormation template:

Resources:
  BuildProject:
    Type: AWS::CodeBuild::Project
    Properties:
      Name:
        Fn::Sub: ${AWS::StackName}-BuildProject
      Description: Build project
      Source:
        Type: S3
        Location:
          Fn::Sub: ${ArtifactsBucket}/${ArtifactKey}
      Artifacts:
        Type: NO_ARTIFACTS
      Environment:
        Type: LINUX_CONTAINER
        ComputeType: BUILD_GENERAL1_SMALL
        Image: aws/codebuild/amazonlinux2-x86_64-standard:4.0
        EnvironmentVariables:
          - Name: REGION
            Value:
              Ref: AWS::Region
          - Name: REPOSITORY_URI
            Value:
              Fn::GetAtt: ECRRepository.RepositoryUri
        PrivilegedMode: true
      Cache:
        Type: LOCAL
        Modes:
          - LOCAL_DOCKER_LAYER_CACHE
      ServiceRole:
        Fn::GetAtt: CodeBuildServiceRole.Arn
      TimeoutInMinutes: 20

The figure below shows three builds with the identical source artifact. The first build and the second build were invoked in a row, and the third build was invoked about two hours after the second build. The source artifact was never changed.

CodeBulid build history

The second build finished very quickly since Docker used layer cache fully and skipped all the build steps, as there was no change on the source artifact.

However, the third build took as long as the first build, which shows the Docker layer cache had expired and never used.

How can I make Docker layer cache permanent to reduce build time for all the future builds?

profile picture
HS
已提问 8 个月前320 查看次数
1 回答
0
已接受的回答

Hi,

Local CodeBuild cache is best effort and can be erased between builds.

What people usually do to circumvent local cache issue is to build a cache layer in ECR

See https://github.com/aws/aws-codebuild-docker-images/issues/26#issuecomment-370177343

version: 0.2
phases:
  pre_build:
    commands:
      - docker version
      - $(aws ecr get-login --no-include-email)
      - docker pull $CONTAINER_REPOSITORY_URL:$REF_NAME || true
  build:
    commands:
      - docker build --cache-from $CONTAINER_REPOSITORY_URL:$REF_NAME --tag $CONTAINER_REPOSITORY_URL:$REF_NAME --tag $CONTAINER_REPOSITORY_URL:ref-$CODEBUILD_RESOLVED_SOURCE_VERSION .
  post_build:
    commands:
      - docker push $CONTAINER_REPOSITORY_URL

Re. docker build --cache-from, see https://docs.docker.com/build/cache/

Best,

Didier

profile pictureAWS
专家
已回答 8 个月前
profile picture
专家
已审核 8 个月前
  • Thanks a lot! This works for me.

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则