- Published on
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:
How Does an External Cache Reduce AWS CodeBuild Time?
Example code from AWS
yamlversion: 0.2env:variables:CONTAINER_REPOSITORY_URL: account-ID.dkr.ecr.region.amazonaws.com/amazon_linux_codebuild_imageTAG_NAME: latestphases:install:runtime-versions:docker: 19pre_build:commands:- $(aws ecr get-login --no-include-email)- docker pull $CONTAINER_REPOSITORY_URL:$TAG_NAME || truebuild:commands:- docker build --cache-from $CONTAINER_REPOSITORY_URL:$TAG_NAME --tag$CONTAINER_REPOSITORY_URL:$TAG_NAME .post_build:commands:- docker push $CONTAINER_REPOSITORY_URLYAML
My experience result - reduced AWS CodeBuild time by more than 50%
Cache is used in Docker build log
The actual code changes
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
- Published on