What is docker build --no-cache
The docker build --no-cache option forces Docker to build an image from scratch without using any previously cached layers. This ensures that every instruction in your Dockerfile is executed freshly.
How Docker Layer Caching Works
Docker builds images in layers. Each instruction in the Dockerfile creates a layer, and Docker caches these layers to speed up future builds.
For example:
FROM ubuntu→ base layerRUN apt-get update→ cached layerCOPY . /app→ new layer
If nothing changes in a step, Docker reuses the cached layer instead of rebuilding it.
What Happens When You Disable Cache
When you use --no-cache:
- All layers are rebuilt from scratch
- No previously cached layers are reused
- Every instruction runs again, even if unchanged
Example:
docker build --no-cache -t myapp .This ensures:
- Fresh dependencies
- No stale data from previous builds
When Should You Use --no-cache
Use --no-cache when:
- You suspect outdated dependencies
- Debugging Dockerfile issues
- Ensuring a clean production build
- CI/CD pipelines require fresh builds
Quick Cheat Sheet (Docker Build Without Cache)
| Use Case | Command |
|---|---|
| Build without cache | docker build --no-cache -t myapp . |
| Build with latest base image | docker build --no-cache --pull -t myapp . |
| Debug build logs | docker build --no-cache --progress=plain . |
| Tag image during build | docker build --no-cache -t myapp:v1 . |
| Build specific Dockerfile | docker build --no-cache -f Dockerfile.dev . |
Docker Build Cache Explained (Important for Beginners)
How Docker Reuses Layers
Docker checks each instruction in your Dockerfile:
- If the instruction hasn’t changed → reuse cache
- If changed → rebuild from that step onward
Example:
RUN apt-get update && apt-get install -y curlIf this line remains unchanged, Docker may reuse the cached layer even if newer packages are available.
Cache Invalidation Rules
Cache is invalidated when:
- Dockerfile instruction changes
- Files copied via
COPYorADDchange - Base image changes (with
--pull) - Build arguments (
ARG) change
Once a layer is invalidated:
- All subsequent layers are rebuilt
Common Misconceptions About Docker Cache
- Cache does NOT automatically fetch latest packages
apt-get updatemay still use cached results- Changing one file can invalidate multiple layers
- Cache improves speed but can introduce stale builds
Understanding these helps avoid unexpected behavior during builds
Understanding Docker Cache with Practical Example
To clearly understand how Docker caching works, let’s walk through a simple example.
Consider the following Dockerfile:
FROM ubuntu:16.04
RUN apt-get update && apt-get install -y vim
CMD ["echo", "Hello user"]First Build (No Cache Available)
When you build the image for the first time:
docker build -t test-cache:1.0 .Docker executes every step:
- Pulls the base image
- Runs package update and installs
vim - Creates new image layers
This process takes more time because nothing is cached yet.
After building, run the container:
docker run test-cache:1.0Output:
Hello userSecond Build (Using Cache)
Now, build the same image again without making any changes:
docker build -t test-cache:1.0 .This time, Docker reuses previously built layers:
- You will see
Using cachein the output - Build completes much faster
- No commands are re-executed
This happens because:
- The Dockerfile has not changed
- Docker detects identical instructions and reuses cached layers
How Cache Improves Build Performance
Docker caching helps by:
- Avoiding repeated execution of identical steps
- Reducing build time significantly
- Improving efficiency in development workflows
However, it may also lead to:
- Outdated packages (e.g.,
apt-get updatenot refreshing) - Stale application builds
Forcing a Fresh Build with --no-cache
To ignore cached layers and rebuild everything:
docker build --no-cache -t test-cache:1.0 .Now Docker will:
- Execute all instructions again
- Reinstall dependencies
- Create fresh image layers
You will notice that:
Using cacheno longer appears- Build time increases
- All steps run from scratch
Common Mistakes and Troubleshooting
Why Changes Are Not Reflected
- Docker reused cached layers
- Files were not copied properly
- Build context not updated
Fix:
- Use
--no-cache - Verify
COPYpaths - Check
.dockerignore
Cache Still Appearing to Be Used
Even with --no-cache, issues can occur due to:
- External dependencies not updating
- Base image not refreshed
Solution:
docker build --no-cache --pull .Debugging Build Logs Effectively
Use:
docker build --progress=plain .Also helpful: View container logs in real time
docker build --no-cache vs Other Options
Difference Between --no-cache and --pull
| Option | Purpose |
|---|---|
--no-cache | Rebuild all layers |
--pull | Fetch latest base image |
Using --pull for Updated Base Images
Example:
docker build --pull -t myapp .This ensures:
- Latest OS patches
- Updated base dependencies
Combining Flags for Better Results
Best practice:
docker build --no-cache --pull -t myapp .Use this when:
- Security updates are critical
- Production builds require freshness
Summary
- Docker caching improves speed but can cause stale builds
- Use
--no-cacheselectively for debugging and clean builds - Combine with
--pullfor complete freshness - Optimize Dockerfile to avoid unnecessary cache invalidation
- Use
.dockerignoreto improve build efficiency
For broader container management: Manage Docker containers with examples


