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
demandé il y a 8 mois320 vues
1 réponse
0
Réponse acceptée

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
répondu il y a 8 mois
profile picture
EXPERT
vérifié il y a 8 mois
  • Thanks a lot! This works for me.

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions