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
asked 7 months ago303 views
1 Answer
0
Accepted Answer

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
EXPERT
answered 7 months ago
profile picture
EXPERT
reviewed 7 months ago
  • Thanks a lot! This works for me.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions