Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: cache invalidation improvements #19617

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 6 additions & 19 deletions content/build/cache/_index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Optimizing builds with cache management
title: Docker build cache
description: Improve your build speed with effective use of the build cache
keywords: build, buildx, buildkit, dockerfile, image layers, build instructions, build context
aliases:
Expand All @@ -9,7 +9,7 @@ aliases:
When you build the same Docker image multiple times, knowing how to optimize
the build cache is a great tool for making sure the builds run fast.

## How does the build cache work?
## How the build cache works

Understanding Docker's build cache helps you write better Dockerfiles that
result in faster builds.
Expand Down Expand Up @@ -48,23 +48,9 @@ And that's the Docker build cache in a nutshell. Once a layer changes, then all
downstream layers need to be rebuilt as well. Even if they wouldn't build
anything differently, they still need to re-run.

> **Note**
>
> Suppose you have a step in your Dockerfile
> to upgrade all the software packages in your
> Debian-based image to the latest version:
>
> ```dockerfile
> RUN apt-get update && apt-get upgrade -y
> ```
>
> This doesn't mean that the images you build are always up to date. Rebuilding
> the image on the same host one week later will still get you the same packages
> as before. The only way to force a rebuild is by making sure that a layer
> before it has changed, or by clearing the build cache using
> [`docker builder prune`](../../reference/cli/docker/builder/prune.md).

## How can I use the cache efficiently?
For more details about how cache invalidation works, see [Cache invalidation](invalidation.md).

## Optimizing how you use the build cache

Now that you understand how the cache works, you can begin to use the cache to
your advantage. While the cache will automatically work on any `docker build`
Expand Down Expand Up @@ -287,5 +273,6 @@ of continuing.)

For more information on using cache to do efficient builds, see:

- [Cache invalidation](invalidation.md)
- [Garbage collection](garbage-collection.md)
- [Cache storage backends](./backends/index.md)
2 changes: 1 addition & 1 deletion content/build/cache/backends/azblob.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ The environment variables are read from the server, not the Buildx client.

## Further reading

For an introduction to caching see [Optimizing builds with cache](../_index.md).
For an introduction to caching see [Docker build cache](../_index.md).

For more information on the `azblob` cache backend, see the
[BuildKit README](https://github.com/moby/buildkit#azure-blob-storage-cache-experimental).
2 changes: 1 addition & 1 deletion content/build/cache/backends/gha.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ For example:

## Further reading

For an introduction to caching see [Optimizing builds with cache](../_index.md).
For an introduction to caching see [Docker build cache](../_index.md).

For more information on the `gha` cache backend, see the
[BuildKit README](https://github.com/moby/buildkit#github-actions-cache-experimental).
Expand Down
2 changes: 1 addition & 1 deletion content/build/cache/backends/inline.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ $ docker buildx build --push -t <registry>/<image> \

## Further reading

For an introduction to caching see [Optimizing builds with cache](../index.md).
For an introduction to caching see [Docker build cache](../_index.md).

For more information on the `inline` cache backend, see the
[BuildKit README](https://github.com/moby/buildkit#inline-push-image-and-cache-together).
2 changes: 1 addition & 1 deletion content/build/cache/backends/local.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ $ docker buildx build --push -t <registry>/<image> \

## Further reading

For an introduction to caching see [Optimizing builds with cache](../_index.md).
For an introduction to caching see [Docker build cache](../_index.md).

For more information on the `local` cache backend, see the
[BuildKit README](https://github.com/moby/buildkit#local-directory-1).
2 changes: 1 addition & 1 deletion content/build/cache/backends/registry.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fail, but the build continues.

## Further reading

For an introduction to caching see [Optimizing builds with cache](../_index.md).
For an introduction to caching see [Docker build cache](../_index.md).

For more information on the `registry` cache backend, see the
[BuildKit README](https://github.com/moby/buildkit#registry-push-image-and-cache-separately).
2 changes: 1 addition & 1 deletion content/build/cache/backends/s3.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ authentication using environment variables and credentials file.

## Further reading

For an introduction to caching see [Optimizing builds with cache](../_index.md).
For an introduction to caching see [Docker build cache](../_index.md).

For more information on the `s3` cache backend, see the
[BuildKit README](https://github.com/moby/buildkit#s3-cache-experimental).
89 changes: 89 additions & 0 deletions content/build/cache/invalidation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
title: Build cache invalidation
description: Dig into the details abouw how cache invalidation works for Docker's build cache
keywords: build, buildx, buildkit, cache, invalidation, cache miss
---

When building an image, Docker steps through the instructions in your
Dockerfile, executing each in the order specified. For each instruction, Docker
checks whether it can reuse the instruction from the build cache.

## General rules

The basic rules of build cache invalidation are as follows:

- Starting with a parent image that's already in the cache, the next
instruction is compared against all child images derived from that base
image to see if one of them was built using the exact same instruction. If
not, the cache is invalidated.

- In most cases, simply comparing the instruction in the Dockerfile with one
of the child images is sufficient. However, certain instructions require more
examination and explanation.

- For the `ADD` and `COPY` instructions, the modification time and size file
metadata is used to determine whether cache is valid. During cache lookup,
cache is invalidated if the file metadata has changed for any of the files
involved.

- Aside from the `ADD` and `COPY` commands, cache checking doesn't look at the
files in the container to determine a cache match. For example, when processing
a `RUN apt-get -y update` command the files updated in the container
aren't examined to determine if a cache hit exists. In that case just
the command string itself is used to find a match.

Once the cache is invalidated, all subsequent Dockerfile commands generate new
images and the cache isn't used.

If your build contains several layers and you want to ensure the build cache is
reusable, order the instructions from less frequently changed to more
frequently changed where possible.

## RUN instructions

The cache for `RUN` instructions isn't invalidated automatically between builds.
Suppose you have a step in your Dockerfile to install `curl`:

```dockerfile
FROM alpine:{{% param "example_alpine_version" %}} AS install
RUN apk add curl
```

This doesn't mean that the version of `curl` in your image is always up-to-date.
Rebuilding the image one week later will still get you the same packages as before.
To force a re-execution of the `RUN` instruction, you can:

- Make sure that a layer before it has changed
- Clear the build cache ahead of the build using
[`docker builder prune`](../../reference/cli/docker/builder/prune.md)
- Use the `--no-cache` or `--no-cache-filter` options

The `--no-cache-filter` option lets you specify a specific build stage to
invalidate the cache for:

```console
$ docker build --no-cache-filter install .
```

## Build secrets

The contents of build secrets are not part of the build cache.
Changing the value of a secret doesn't result in cache invalidation.

If you want to force cache invalidation after changing a secret value,
you can pass a build argument with an arbitrary value that you also change when changing the secret.
Build arguments do result in cache invalidation.

```dockerfile
FROM alpine
ARG CACHEBUST
RUN --mount=type=secret,id=foo \
TOKEN=$(cat /run/secrets/foo) ...
```

```console
$ TOKEN=verysecret docker build --secret id=foo,env=TOKEN --build-arg CACHEBUST=1 .
```

Properties of secrets such as IDs and mount paths do participate in the cache
checksum, and result in cache invalidation if changed.
2 changes: 1 addition & 1 deletion content/build/guide/layers.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ work at build time.

Related information:

- [Optimizing builds with cache](../cache/index.md)
- [Docker build cache](../cache/_index.md)
- [Dockerfile best practices](../../develop/develop-images/dockerfile_best-practices.md)

## Next steps
Expand Down
35 changes: 4 additions & 31 deletions content/develop/develop-images/guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,37 +86,10 @@ When building an image, Docker steps through the instructions in your
Dockerfile, executing each in the order specified. For each instruction, Docker
checks whether it can reuse the instruction from the build cache.

The basic rules of build cache invalidation are as follows:

- Starting with a parent image that's already in the cache, the next
instruction is compared against all child images derived from that base
image to see if one of them was built using the exact same instruction. If
not, the cache is invalidated.

- In most cases, simply comparing the instruction in the Dockerfile with one
of the child images is sufficient. However, certain instructions require more
examination and explanation.

- For the `ADD` and `COPY` instructions, the modification time and size file
metadata is used to determine whether cache is valid. During cache lookup,
cache is invalidated if the file metadata has changed for any of the files
involved.

- Aside from the `ADD` and `COPY` commands, cache checking doesn't look at the
files in the container to determine a cache match. For example, when processing
a `RUN apt-get -y update` command the files updated in the container
aren't examined to determine if a cache hit exists. In that case just
the command string itself is used to find a match.

Once the cache is invalidated, all subsequent Dockerfile commands generate new
images and the cache isn't used.

If your build contains several layers and you want to ensure the build cache is
reusable, order the instructions from less frequently changed to more
frequently changed where possible.

For more information about the Docker build cache and how to optimize your
builds, see [cache management](../../build/cache/_index.md).
Understanding how the build cache works, and how cache invalidation occurs,
is critical for ensuring faster builds.
For more information about the Docker build cache and how to optimize your builds,
see [Docker build cache](../../build/cache/_index.md).

## Pin base image versions

Expand Down
4 changes: 3 additions & 1 deletion data/toc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1819,7 +1819,9 @@ Manuals:
- sectiontitle: Cache
section:
- path: /build/cache/
title: Optimizing builds with cache
title: Overview
- path: /build/cache/invalidation/
title: Cache invalidation
- path: /build/cache/garbage-collection/
title: Garbage collection
- sectiontitle: Cache backends
Expand Down
Loading