Clean Image Docker Build Using “–no-cache”

The “docker build” command is particularly used to build the Docker images. Docker images are simple files that instruct and guide containers on managing and deploying applications in a container. Developers may want to rebuild the image after updating the project code or dockerfile. However, while rebuilding an image using “docker build”, the command will only update the modified portion of the code and generate the entire image using the previous image cache.

But in some scenarios, it can be required to update all project dependencies of the docker image along with the code. For this purpose, avoid the previous image cache and cleanly build the image.

This blog will illustrate:

How to Build a Clean Docker Image Using the “–no-cache” Option?

To build the Docker image without using the previous image cache and to update all project dependencies, the “–no-cache” option is used along with the “docker build” command.

Look at the provided instructions to clean build the Docker image using the “–no-cache” option.

Step 1: Create Dockerfile

First, create a simple file named “Dockerfile”. Then, paste the following instructions into the file:

FROM python:3.6
WORKDIR /src/app
COPY . .
CMD [ "python", "./" ]

Step 2: Create Program File

In the next step, create a simple program file that contains simple Python program:

print("Hello, I am building first Python application")

After that, launch the Visual Studio Code terminal to execute Docker commands. Moreover, users can directly use the Windows terminals to execute Docker commands:

Step 3: Build Docker Image

Next, build the new Docker image using the “docker build” command. Here, the image name is specified by the “-t” option:

> docker build -t python-img .

Step 4: Update Program File

Next, update the program file by making some changes in the code. For instance, we have changed the “print” statement:

print("Hello, Welcome to Linuxhint tutorial")

Step 5: Rebuild Docker Image

Again, utilize the “docker build” command to rebuild the Docker image:

> docker build -t python-img .

It can be noticed that only the updated portion is rebuilt and for entire image command uses the previous image cache:

Step 6: Clean Rebuild the Docker Image Using “–no-cache” Option

To clean build the Docker image, you can utilize the “–no-cache” option. This option restricts the Docker engine from using a previous image cache and completely rebuilds the Docker image:

> docker build --no-cache -t python-img .

Alternative Method: Clean Build the Docker Image

Alternative method to clean build the Docker image without using the previous image cache is first to prune the system using the “system prune” command. This command will remove all unused and dangling images, containers, and associated networks. Then, rebuild the Docker image using the “docker build” command.

Step 1: Prune System

To prune the system, run the given command:

> docker system prune

Step 2: Clean Build Docker Image

Now, rebuild the docker image with the help of the “docker build –no-cache” command:

> docker build --no-cache -t python-img .

Step 3: Run Python Image

To run the Docker image, utilize the “docker run <image-name>” command:

> docker run python-img

Note: It is not recommended to prune the system as it can remove all unused and stopped containers and images that may be required in future.


To build a clean image without using Docker’s previous image cache, the “–no-cache” option can be utilized along with the “docker build” command. The “–no-cache” option prevents the Docker engine from accessing a prior image cache and cleanly builds the image. This blog has demonstrated how to cleanly build an image using the “–no-cache” option.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.