Docker Layer Cache doesn't seem to be working in AWS CodeBuild

0

I have been trying to speed up Docker image building using multistage builds and Docker Layer Cache. However, all the steps get executed at every build and the layer cache doesn't seem to work at all. Here is my build spec, can anyone help identify anything I might be missing here. I am using the build/6.0 image and have the privileged mode turned on as recommended in the docs.

version: 0.2

phases:
  pre_build:
    commands:
      - echo Logging into Amazon ECR
      - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ECR_IMAGE_URL
      - export DOCKER_BUILDKIT=1
      - docker pull $AWS_ECR_IMAGE_URL:compile-stage || true
      - docker pull $AWS_ECR_IMAGE_URL:$IMAGE_TAG || true
  build:
    commands:
      - echo Building the Docker image ...
      - export PYPI_USER=aws
      - export PYPI_TOKEN=`aws codeartifact get-authorization-token --domain $PYPI_DOMAIN --domain-owner $ACCOUNT_ID --query authorizationToken --output text`
      - cd production_code
      - |
        docker build --target compile-image \
        --build-arg BUILDKIT_INLINE_CACHE=1 \
        --build-arg PYPI_USER \
        --build-arg PYPI_TOKEN \
        --cache-from=$AWS_ECR_IMAGE_URL:compile-stage \
        --tag $AWS_ECR_IMAGE_URL:compile-stage .
      - |
        docker build --target runtime-image \
        --build-arg BUILDKIT_INLINE_CACHE=1 \
        --cache-from=$AWS_ECR_IMAGE_URL:compile-stage \
        --cache-from=$AWS_ECR_IMAGE_URL:$IMAGE_TAG \
        --tag $AWS_ECR_IMAGE_URL:$IMAGE_TAG .
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the compile image
      - docker push $AWS_ECR_IMAGE_URL:compile-stage
      - echo Pushing the runtime image
      - docker push $AWS_ECR_IMAGE_URL:$IMAGE_TAG
asked a year ago1269 views
2 Answers
0

I thought it would be cached if privileged mode was enabled.
I also don't think I see anything wrong with the buildspec.yml.
https://docs.aws.amazon.com/codebuild/latest/userguide/build-caching.html

Put the following command in the build phase of buildspec.yml to make sure CodeBuild is not changing hosts.
If the host is different, the cache is not used.

uname -n

https://docs.aws.amazon.com/codebuild/latest/userguide/build-caching.html#caching-local

Local caching stores a cache locally on a build host that is available to that build host only. This is a good option for intermediate to large build artifacts because the cache is immediately available on the build host. This is not the best option if your builds are infrequent. This means that build performance is not impacted by network transfer time.

profile picture
EXPERT
answered a year ago
  • Do you mean to say, the pre-build and the build stages are actually run in different host instances?

  • It means that different hosts may have been used in the first and second builds.

0

I'm seeing similar issues. The --cache-from argument does not appear to do anything inside CodeBuild. Have you been able to resolve the issue?

answered 10 months ago
  • Nope. I have given up on this.

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