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.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠