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
質問済み 1年前1309ビュー
2回答
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
エキスパート
回答済み 1年前
  • 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?

回答済み 1年前
  • Nope. I have given up on this.

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ