User Agents

Docker Build No-Cache: What It Does and When to Use It

Running docker build --no-cache tells Docker to ignore all cached layers from previous builds and rebuild every instruction from scratch. This forces a completely fresh build, which is useful when you suspect stale cache is causing problems or when you need to ensure the latest versions of dependencies are fetched.

What Docker Build Cache Is and Why It Exists

Docker builds images by executing each instruction in a Dockerfile as a separate layer. To speed up repeated builds, Docker caches the result of each layer. When you rebuild an image, Docker checks whether the instruction and its inputs (like the base image, files copied, or commands run) have changed since the last build. If nothing has changed, Docker reuses the cached layer instead of executing the instruction again. This can dramatically reduce build times, especially for large projects with many dependencies.

The cache is keyed on the exact content of the instruction and the state of the filesystem at that point. For example, a RUN apt-get update instruction will be cached after the first run, so subsequent builds skip the update step unless the cache is invalidated. This is generally desirable, but it can lead to outdated packages if the cache is not refreshed periodically.

How --no-cache Works

When you pass the --no-cache flag to docker build, Docker does not look for cached layers at all. It executes every instruction in the Dockerfile as if it were the first build. This means:

  • All RUN commands are executed again, including package manager updates and installations.
  • All COPY and ADD instructions re-copy files from the build context, even if they haven't changed.
  • The base image is pulled again if it's not already present locally, but note that --no-cache does not force a pull of a newer version of the base image if the tag already exists locally. To update the base image, you need to explicitly pull it or use --pull.

It's important to understand that --no-cache only affects the build cache on the local machine. It does not affect any remote cache or registry cache that might be used in conjunction with BuildKit or other build backends.

When to Use --no-cache

There are several scenarios where --no-cache is appropriate:

  • Debugging build issues: If a build fails or produces unexpected results, using --no-cache can help determine whether the problem is due to a stale cache layer.
  • Ensuring fresh dependencies: When you need to make sure that package managers fetch the latest versions of dependencies, such as running apt-get update or npm install without a lockfile.
  • Security updates: To ensure that the base image and all installed packages include the latest security patches, especially before deploying to production.
  • After changing build context in ways not tracked by cache: Although Docker's cache is generally reliable, there are edge cases where files outside the immediate context or external resources change without affecting the cache key.

However, using --no-cache indiscriminately can lead to longer build times and increased load on package repositories. It's best used selectively.

How to Use --no-cache with docker build

The basic syntax is straightforward:

docker build --no-cache -t myimage:latest .

This command builds an image tagged myimage:latest from the current directory, ignoring all cached layers. You can combine --no-cache with other flags like --pull to also force pulling the latest base image:

docker build --no-cache --pull -t myimage:latest .

If you are using BuildKit (the default in newer Docker versions), the same flag applies. BuildKit may also use a different cache mechanism, but --no-cache still disables cache reuse for the build.

Common Pitfalls and Misconceptions

One common misconception is that --no-cache will always pull the latest version of the base image. In fact, it only rebuilds layers; if the base image tag (e.g., ubuntu:latest) is already present locally, Docker will use the local version unless you also specify --pull. To ensure you have the latest base image, use both flags.

Another pitfall is that --no-cache can significantly increase build times, especially for large images with many layers. It also increases network usage and may hit rate limits on package repositories. Therefore, it's not recommended for every build, but rather for specific situations like CI/CD pipelines where reproducibility is critical, or when you suspect cache corruption.

Additionally, --no-cache does not remove existing images or dangling layers; it only affects the build process. To clean up unused images and build cache, you can use docker image prune and docker builder prune.

Alternatives to --no-cache

In some cases, you might not need to disable the entire cache. Consider these alternatives:

  • Invalidate specific layers: Modify the Dockerfile to change the cache key for a particular instruction, such as adding an argument or changing a comment. This forces a rebuild from that point onward without disabling cache for earlier layers.
  • Use build arguments: Pass a build argument with a changing value (like a timestamp) to bust the cache for specific instructions.
  • Leverage multi-stage builds: Separate dependency installation from application code to minimize cache invalidation.
  • Use --pull alone: If you only need to update the base image, --pull might suffice without disabling all cache.

These approaches can provide more granular control and preserve build speed where possible.

Frequently Asked Questions

Does --no-cache also pull the latest base image?

No, --no-cache only disables the build cache. To pull the latest version of the base image, you must also use the --pull flag. Without --pull, Docker uses the locally available base image even if a newer version exists in the registry.

Can I use --no-cache with Docker Compose?

Yes, Docker Compose supports the --no-cache flag with the build command. For example: docker compose build --no-cache. This applies the flag to all services being built.

Does --no-cache delete existing images?

No, --no-cache does not remove any existing images or layers. It only affects the build process by not using cached layers. To clean up unused images, use docker image prune.

Is --no-cache the same as --pull?

No, they are different. --no-cache disables the build cache for all layers, while --pull forces Docker to attempt to pull a newer version of the base image from the registry. They can be used together.

Why does my build still use cache even with --no-cache?

If you're using BuildKit, --no-cache should disable cache. However, if you have external cache sources configured (like a registry cache), those might still be used. Also, ensure you're not using a different build command or tool that ignores the flag.

When should I avoid using --no-cache?

Avoid using --no-cache for routine builds where speed is important, as it significantly increases build time. Use it only when you need to ensure a completely fresh build, such as for security updates or debugging cache-related issues.