logo
February 19, 2023

How to reduce Docker image build time on AWS CodeBuild using an external cache

How to reduce the Docker build time 50% with 2 line of change code?

Image is high-level architecture of this solution from AWS blog

How to reduce the Docker build time 50% with 2 line of change code?

Background

AWS CodeBuild is a fully managed service that allows you to build and test your software applications. However, the build process can take a significant amount of time, especially when building Docker images. In this blog, we will explore how to reduce AWS CodeBuild time by up to 50% using an external cache via AWS ECR.

What is an External Cache?

An external cache is a way to store commonly used files that can be reused in subsequent builds. When building Docker images on AWS CodeBuild, you can use an external cache to store Docker layers, which can be reused across multiple builds. By using an external cache, you can significantly reduce the time it takes to build Docker images, thereby reducing AWS CodeBuild time.

How to Set Up an External Cache via AWS ECR?

Amazon Elastic Container Registry (ECR) is a fully managed container registry that makes it easy to store, manage, and deploy Docker container images. To set up an external cache via ECR, follow these steps:

  • Create an ECR repository: In the AWS Management Console, create an ECR repository to store your Docker images.
  • Configure AWS CodeBuild: In the AWS CodeBuild console, configure your build project to use the ECR repository as an external cache. When you run a build, AWS CodeBuild will automatically check the ECR repository for cached Docker layers before building new layers.
  • How Does an External Cache Reduce AWS CodeBuild Time?

    Example code from AWS

    yaml
    version: 0.2
    env:
    variables:
    CONTAINER_REPOSITORY_URL: account-ID.dkr.ecr.region.amazonaws.com/amazon_linux_codebuild_image
    TAG_NAME: latest
    phases:
    install:
    runtime-versions:
    docker: 19
    pre_build:
    commands:
    - $(aws ecr get-login --no-include-email)
    - docker pull $CONTAINER_REPOSITORY_URL:$TAG_NAME || true
    build:
    commands:
    - docker build --cache-from $CONTAINER_REPOSITORY_URL:$TAG_NAME --tag
    $CONTAINER_REPOSITORY_URL:$TAG_NAME .
    post_build:
    commands:
    - docker push $CONTAINER_REPOSITORY_URL
    YAML

    My experience result - reduced AWS CodeBuild time by more than 50%

    A visual depiction of what is being written about

    Cache is used in Docker build log

    A visual depiction of what is being written about

    The actual code changes

    A visual depiction of what is being written about

    By using an external cache via AWS ECR, you can reduce AWS CodeBuild time by up to 50%. When building a Docker image, AWS CodeBuild can take advantage of cached layers stored in the ECR repository. Instead of building each layer from scratch, AWS CodeBuild can retrieve the layers from the ECR repository, significantly reducing the time it takes to build the Docker image.

    Conclusion

    In conclusion, reducing AWS CodeBuild time is critical for software development teams to improve their overall efficiency. Using an external cache via AWS ECR can significantly reduce AWS CodeBuild time by up to 50%. With this setup, AWS CodeBuild can retrieve cached Docker layers stored in the ECR repository, resulting in faster build times. As a result, software development teams can spend more time creating quality applications and less time waiting for builds to complete.

    Reference

    https://aws.amazon.com/blogs/devops/reducing-docker-image-build-time-on-aws-codebuild-using-an-external-cache/