Docker

What is the Purpose of the “docker build –no-cache” Command?

While working on Docker, when users create a Docker image using a Docker file, Docker caches the intermediate images created during the build process. This can be helpful for speeding up the build process when users have not made any changes to the Docker file. However, sometimes users may want to ensure that the build process starts from scratch and not use any previous image cache. In this situation, the “docker build” command can be used with the “–no-cache” option.

This article will illustrate:

What is the Purpose of the “docker build –no-cache” Command?

The “docker build –no-cache” command is utilized for building a Docker image by using a Docker file without utilizing any previous image cache. This ensures that the build process starts from scratch and does not use any previous image cache.

This can be helpful in situations where users have made important modifications to the Docker file or if users are facing problems with a previously cached image. The “–no-cache” option forces Docker to build the entire image again from the beginning, without utilizing any cached intermediate images.

How does the “docker build –no-cache” Command Work in Docker?

To see the working of the “docker build –no-cache” Command, check out the below-provided instructions.

Step 1: Create a Program File
On Visual Studio Code, create a new program file named “index.html” and paste the given-provided snippet into it:

<!DOCTYPE html>
<html>
<head>
    <title>LinuxHint</title>
</head>

</html>

Step 2: Create a Docker file
Next, make a new file called “Dockerfile” and insert the below-provided code into it to containerize the HTML program:

FROM nginx:latest

COPY index.html /usr/share/nginx/html/index.html

In the above code:

  • FROM” instruction is utilized to define a base image for the container.
  • COPY” command pastes the “index.html” file into the container path.

Step 3: Build Docker Image
After that, create a new Docker image by utilizing the “docker build” command along with the “-t” flag to define the image name. For instance, we have specified “myhtml-img” name for the image:

docker build -t myhtml-img .

The Docker image has been created successfully.

Step 4: Modify the Program File
Now, make some changes to the program file. For instance, we have added the “<body></body>” section in our program:

<!DOCTYPE html>
<html>
<head>
    <title>Linuxhint</title>
</head>
<body>
    <h1>Hello, World!</h1>
    <p>Welcome to Linuxhint tutorial</p>
</body>

</html>

Step 5: Rebuild Docker Image
Execute the below-listed command again to build the Docker image again with recent changes:

docker build -t myhtml-img .

In the above output, the times in seconds show that only the modified part is rebuilt, and the command used a previous image cache for the entire image.

Step 6: Rebuild Docker Image Without Cached Layer
Use the “–no-cache” option with the previous command to restrict Docker from utilizing a previous image cache and entirely rebuilds the Docker image:

docker build --no-cache -t myhtml-img .

This command has created a new image from scratch without using any cached layers from previous builds.

Conclusion

The “docker build –no-cache” command is utilized to build a new image from a Docker file without using any cached layers from previous builds. This ensures that the image is built from scratch and reflects the latest changes in the Docker file and the base image. This article has illustrated the purpose of the “docker build –no-cache” command.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.