How to speed up Docker builds in GitHub Actions
Are your Docker builds slow in GitHub Actions? Here's how to speed it up with the built-in GitHub Actions cache.
Are your Docker builds slow in GitHub Actions? Here's how to speed it up with the built-in GitHub Actions cache.
Official Docker Action
Docker published an official GitHub Actions cache integration along with an official GitHub Actions plugin.
Here's how to use both and turn on the GitHub Actions cache to speed up your Docker builds. The trick is to set cache-from and cache-to to type=gha
A complete working example is here at
https://github.com/deptagency/engineering-blog-github-actions/blob/main/.github/workflows/docker-nextjs.yml
How do you know that the cache is working? The Docker build output will tell you. Your first Docker build will be slower because it's populating the cache. For subsequent builds, you will see a Docker output similar to:
#10 [deps 1/4] RUN apk add --no-cache libc6-compat
#10 CACHED
See sample output here
https://github.com/deptagency/engineering-blog-github-actions/actions/runs/5895631079/job/15991811696#step:7:176
Alternatives
GHA local cache
If you want more control over your GitHub cache, you can use Docker local cache. Just set cache-from and cache-to to type=local
.
A complete working example is here at
https://github.com/deptagency/engineering-blog-github-actions/blob/main/.github/workflows/docker-nextjs-actions-cache.yml
Registry cache
You can use the container registry, such as GitHub Container Registry or AWS ECR, to store the image build cache.
For this example, we will use the AWS ECR by setting the cache to type=registry
A complete working example for GHCR is here at
https://github.com/deptagency/engineering-blog-github-actions/blob/main/.github/workflows/docker-nextjs-registry-cache-ghcr.yml
A complete working example for AWS ECR is here at
https://github.com/deptagency/engineering-blog-github-actions/blob/main/.github/workflows/docker-nextjs-registry-cache.yml