ThisCalculator

Everyday

Docker update optimization trick: how to speed up

Updated · Standard formula, shown below

Estimated time saved 4.50 min
New estimated time
5.50 min
Result
Current build time
10 min
You entered
Layers cached
50 %
You entered

This trick reduces Docker update time by reusing cached image layers and avoiding unnecessary rebuilds. You enter your Dockerfile and build context, and the calculator below estimates the potential time savings based on your current build strategy.

Formula

Estimated time saved = Current build time × (Percentage of layers cached / 100) × ((Total layers − 1) / Total layers)

Where:

  • Current build time (t) – minutes for a full rebuild without cache.
  • Percentage of layers cached (c) – share of layers that can be reused from a previous build.
  • Total layers (l) – number of layers in the Docker image.

Worked Example

Suppose your build takes 10 minutes, you have 10 layers, and 50% of them are cached. Using the formula:

  1. Multiply current time by cached fraction: 10 × 0.50 = 5 minutes.
  2. Multiply by (layers−1)/layers: 5 × (9/10) = 4.5 minutes saved.
  3. New estimated time = 10 − 4.5 = 5.5 minutes.

The calculator shows 4.5 minutes saved and 5.5 minutes new time.

Why Docker Updates Are Slow

Docker images are built in layers. Each instruction in a Dockerfile (like RUN, COPY, or ADD) creates a new layer. When you update an image, Docker checks each layer against its local cache. If a layer hasn’t changed, it reuses it instead of rebuilding. However, if a single early layer changes, all subsequent layers must be rebuilt because they depend on it. This is why a small change in the base image can trigger a long rebuild.

The Trick: Order Your Instructions by Likelihood of Change

The core optimization is to place instructions that change frequently near the end of the Dockerfile, and instructions that change rarely near the beginning. For example:

  • Start with the base image (FROM) – almost never changes.
  • Install system dependencies (RUN apt-get install) – changes only when you alter the list.
  • Copy your source code (COPY . .) – changes on every commit.
  • Run build steps (RUN npm build) – changes when source changes.

By keeping the frequently changing parts last, you maximize cache reuse. If you copy your source first and then install dependencies, any code change invalidates the dependency layer, forcing a full rebuild.

How to Choose the Right Order

Review your Dockerfile and classify each instruction by how often it changes. Then reorder them from most stable to most volatile. Use a .dockerignore file to exclude files that aren’t needed for the build, such as node_modules or .git, because they can invalidate the COPY layer even if the source didn’t change. Also consider combining multiple RUN commands into one to reduce the number of layers, but be careful – combining too much can reduce cache granularity.

Common Problems and Misconceptions

One common mistake is assuming that cache is always valid. Docker uses the cache only if the instruction and its context are identical. For example, COPY . . will invalidate the cache if any file in the context changes, even if you only changed a comment. Another misconception is that using --no-cache always gives a better image – it doesn’t; it just makes builds slower. Also, be aware that some base images are updated frequently, so pinning to a specific tag (like ubuntu:22.04 instead of ubuntu:latest) gives more predictable caching.

Comparing Build Strategies

There are several ways to handle Docker builds:

  • Single-stage build – simple but often results in large images and less cache reuse.
  • Multi-stage build – uses multiple FROM statements to copy only what’s needed, reducing image size and improving cache efficiency.
  • BuildKit – a newer build engine that supports parallel layer processing and better cache handling. It’s enabled by default in recent Docker versions.
  • External cache – using a registry or a cache backend to share layers across machines, which speeds up CI/CD pipelines.

For most projects, switching to BuildKit and ordering instructions properly gives the biggest improvement.

Safety and Best Practices

Always test your Dockerfile after reordering to ensure the image still works. Avoid using secrets (like passwords) in build arguments because they may be cached in layers. Use environment variables or Docker secrets instead. Also, clean up unused images and build cache regularly to free disk space, but be aware that clearing the cache removes all cached layers, so the next build will be slower.

Frequently Asked Questions

Does this trick work for all Dockerfiles?

It works for most, but if your Dockerfile uses only a few layers or if the base image changes frequently, the benefit is limited. The principle of ordering by change frequency still applies, but the savings may be small.

How do I check which layers are cached?

Run docker build with the default output. Docker will show Using cache for layers it reuses. You can also use docker history to see layer details.

What is BuildKit and do I need it?

BuildKit is a modern build engine that improves cache management and performance. It’s enabled by default in Docker 23.0 and later. You can enable it manually with DOCKER_BUILDKIT=1 docker build on older versions.

Can I use this trick with docker-compose?

Yes, docker-compose uses the same Dockerfile and benefits from the same ordering. Ensure you set DOCKER_BUILDKIT=1 in the environment if needed.

Why does my build still take long even with caching?

If a layer is invalidated, all following layers rebuild. Also, if your build context is large, sending it to the daemon takes time. Use a .dockerignore to reduce context size.

Related

Formula used
in t | Current build time | min | 10 | 0
in c | Layers cached | % | 50 | 0
in l | Total layers | count | 10 | 1
out pace | Estimated time saved | min | t * (c/100) * (l-1)/l | 2
out speed | New estimated time | min | t - (t * (c/100) * (l-1)/l) | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.