Docker No Cache: What It Means and How to Use It
When you run a Docker build with the --no-cache flag, Docker ignores all previously cached layers and rebuilds every step from scratch. This forces a fresh build, ensuring that no stale cached data is used, which is useful when you need to guarantee that the image reflects the latest versions of dependencies or base images.
Why Docker Uses a Build Cache
Docker's build cache is a performance optimization. Each instruction in a Dockerfile (like RUN, COPY, ADD) creates a layer. Docker checks if an identical instruction has been executed before; if so, it reuses the cached layer instead of executing the instruction again. This speeds up rebuilds dramatically, especially for steps that involve downloading packages or compiling code.
The cache is keyed on the instruction itself and the contents of any files it references. For example, a COPY instruction's cache is invalidated if the files being copied change. A RUN instruction's cache is invalidated if the command string changes or if any previous layer changes.
When to Use --no-cache
You should use --no-cache when you suspect the cache is causing issues, such as:
- You've updated a base image tag (e.g.,
FROM ubuntu:latest) and want to ensure the latest version is pulled. - You've changed a package repository or added a new package source, and the cache might be serving old package lists.
- You're debugging a build failure that doesn't occur on a clean build.
- You need to guarantee that the build uses the absolute latest versions of dependencies, even if the Dockerfile hasn't changed.
- You're building in a CI/CD pipeline where reproducibility is critical and you want to avoid any cache-related inconsistencies.
However, using --no-cache comes at the cost of longer build times, as every layer must be rebuilt. It's a trade-off between speed and freshness.
How --no-cache Works Under the Hood
When you pass --no-cache to docker build, Docker does not consult the local layer cache at all. It still pulls the base image if not present locally, but it does not reuse any intermediate layers from previous builds. Each instruction is executed as if it were the first time. This means that even if a layer is identical to one built before, it will be rebuilt and stored as a new layer (though Docker may still deduplicate identical layers in the final image).
It's important to note that --no-cache does not affect the pulling of base images; if the base image is already present locally, Docker will use it without checking for updates unless you also use --pull. To force a fresh pull of the base image, combine both flags: docker build --no-cache --pull.
Common Misconceptions About Docker Cache
One common misconception is that --no-cache will also update the base image. It does not; you need --pull for that. Another is that --no-cache disables caching entirely for the build, but Docker may still cache layers from the base image if they are pulled fresh. Also, --no-cache does not affect the caching of images pulled from registries; it only affects the build cache.
Another misconception is that --no-cache is always necessary for reproducible builds. In practice, a well-written Dockerfile with pinned versions and proper layer ordering can be reproducible without disabling the cache. The cache is deterministic: if inputs are the same, the output is the same.
Alternatives to --no-cache
Instead of disabling the cache entirely, consider these alternatives:
- Use
--pull: This forces Docker to pull the latest version of the base image, which can invalidate the cache for subsequent layers. - Leverage build arguments: Use
ARGto pass a cache-busting value (like a timestamp or commit hash) that changes when you want to invalidate the cache from a certain point. - Use multi-stage builds: Separate dependencies into a stage that changes less frequently, so you can cache that stage while rebuilding only the final stage.
- Use BuildKit's cache mounts: BuildKit (the modern build engine) supports
--mount=type=cachefor package managers, which allows persistent caches across builds without invalidating layers.
These methods can provide more granular control over caching, balancing speed and freshness.
Impact on Build Performance and CI/CD
Using --no-cache can significantly increase build times, especially for large projects with many dependencies. In a CI/CD pipeline, this can lead to longer feedback loops and higher resource consumption. Therefore, it's often better to use cache-busting techniques selectively rather than disabling the cache entirely. For example, you might use --no-cache only for nightly builds or when a dependency update is detected.
Additionally, Docker's cache is local to the machine; in CI environments, you might use a shared cache or a registry-based cache to speed up builds. Tools like docker buildx support caching to a registry, which can be more efficient than rebuilding from scratch.
Frequently Asked Questions
Does --no-cache also update the base image?
No, --no-cache only disables the build cache for instructions in the Dockerfile. To update the base image, you need to use --pull in addition to --no-cache.
Can I use --no-cache with Docker Compose?
Yes, you can run docker compose build --no-cache to rebuild all services without cache. Alternatively, you can set no_cache: true in the build configuration of a service in the Compose file.
What is the difference between --no-cache and --pull?
--no-cache ignores the build cache for all instructions, while --pull forces Docker to pull the latest version of the base image from the registry, even if a local copy exists. They can be used together.
Does --no-cache affect the caching of images pulled from a registry?
No, --no-cache only affects the build cache. Images pulled from a registry are stored locally and are not affected by this flag.
Is --no-cache necessary for reproducible builds?
Not necessarily. A reproducible build can be achieved with a well-structured Dockerfile that pins versions and orders layers correctly. The cache is deterministic, so if inputs are identical, the output will be identical. --no-cache is more about forcing a fresh build when you suspect cache issues.
Can I disable cache for a specific instruction only?
No, --no-cache applies to the entire build. However, you can use build arguments or change the instruction slightly to invalidate the cache for that specific step.