2024-10-25
4 min read

How to Force Docker for a Clean Build of an Image

How to Force Docker for a Clean Build of an Image

Docker is designed to speed up image builds by caching layers. But there are times when you want to ignore that cache entirely, like when you're troubleshooting, rebuilding dependencies, or testing changes that Docker might skip due to caching.

In this short guide, you'll learn how to do a clean Docker image build using simple commands.

Prerequisites

You'll need:

  • Docker installed (tested with Docker Engine 24+)
  • A Dockerfile ready to build
  • A basic understanding of Docker image layers

What Docker's Cache Actually Does

When you run docker build, Docker checks if it can reuse layers from a previous build. Each layer corresponds to a line in your Dockerfile. If nothing has changed, it reuses the cached layer to speed up the process.

This is great for fast iteration but not always what you want.

Option 1: Use --no-cache

The simplest way to force a clean build is by using the --no-cache flag:

docker build --no-cache -t my-app:latest .

What it does:

  • Ignores all previously cached layers
  • Executes every RUN, COPY, and ADD instruction from scratch
  • Ensures you get the latest packages or changes from the filesystem

This is the cleanest and most reliable method.

Option 2: Use --pull to Refresh the Base Image

If you want to keep your build cache but ensure the latest base image (like ubuntu:22.04) is used, add the --pull flag:

docker build --pull -t my-app:latest .

This fetches the latest version of the base image even if it's already available locally.

You can combine it with --no-cache:

docker build --no-cache --pull -t my-app:latest .

Option 3: Invalidate Cache with a Dummy ARG

For more control, you can invalidate the cache at a specific point in the Dockerfile by using a build argument:

# Dockerfile
FROM node:18
ARG CACHE_BREAKER=1
RUN npm install

Then run:

docker build --build-arg CACHE_BREAKER=$(date +%s) -t my-app:latest .

This forces Docker to rebuild everything after the ARG line.

Option 4: Delete Old Images

If you want a truly fresh start, remove the old image first:

docker rmi my-app:latest

Then rebuild:

docker build -t my-app:latest .

Just note this doesn't prevent layer caching, it just removes the named tag. Use it with --no-cache if you want both.


Use --no-cache when you want a full rebuild and --pull when you want fresh base images. Combine them for maximum freshness.

If you need to selectively bust the cache, use a dynamic build argument.

Happy building!

Published: 2024-10-25|Last updated: 2024-10-25T09:00:00Z

Found an issue?