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?
- Alternative Method: Clean Build the Docker Image
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:
WORKDIR /src/app
COPY . .
CMD [ "python", "./pythonapp.py" ]
Step 2: Create Program File
In the next step, create a simple program file that contains simple Python program:
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:
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:
Step 5: Rebuild Docker Image
Again, utilize the “docker build” command to rebuild the Docker image:
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:
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:
Step 2: Clean Build Docker Image
Now, rebuild the docker image with the help of the “docker build –no-cache” command:
Step 3: Run Python Image
To run the Docker image, utilize the “docker run <image-name>” command:
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.
Conclusion
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.