docker build --no-cache | Build docker without cache


Docker

Docker has revolutionized software development and deployment by providing a standardized approach to containerization. The 'docker build' command lies at the core of Docker's functionality, enabling developers to create container images from Dockerfiles with ease. However, frequent rebuilds during the development process can introduce challenges related to caching.

To address this, Docker offers a powerful solution through the 'docker build --no-cache' command. By appending the '--no-cache' flag, developers can override the default caching behavior and trigger a rebuild of the container image from scratch. This ensures a clean and up-to-date environment, eliminating potential issues stemming from outdated dependencies or configurations.

In this article, we will explore the intricacies of 'docker build --no-cache' and its significant impact on accelerating the containerization process. We will examine various scenarios where leveraging '--no-cache' becomes essential, highlighting the advantages, trade-offs, and best practices associated with this command. Whether you are a seasoned Docker user or new to containerization, this guide will equip you with the knowledge to optimize your Docker builds effectively.

In the previous article, we saw how to run the docker in an interactive and terminal mode (https://www.golinuxcloud.com/docker-run-i/) Now, let us get deeper into understanding how to build a docker container using a no-cache parameter.

Let us get started!

 

Why use docker build --no-cache?

The 'docker build --no-cache' command in Docker disables the default caching mechanism during image builds. It is used for two main reasons.

Firstly, it ensures a clean and up-to-date build environment. By starting from scratch without relying on cached layers, developers can avoid issues caused by outdated dependencies or configurations. This is particularly helpful when dealing with rapidly changing components.

Secondly, using '--no-cache' promotes reproducibility and consistency. With caching, subsequent builds may vary based on the cache contents. However, by disabling the cache, developers enforce a consistent build process, ensuring that every build starts from the same baseline and isn't influenced by previous builds or cached layers.

Additionally, 'docker build --no-cache' aids in debugging and troubleshooting. Disabling the cache eliminates caching-related factors, allowing developers to isolate and accurately identify build issues. This is beneficial for diagnosing problems and resolving them efficiently.

 

Building an image with and without cache in docker

Building with cache

Consider a Dockerfile where you want to update and install the vim editor. Below is the Dockerfile for the same.

FROM ubuntu:16.04
RUN apt-get update && apt-get install -y vim
CMD ["echo" , "Hello user"]

Let us try to simply build the docker image using the command shown below.

$ docker build -t test-cache:1.0 -f Dockerfile .

 

Output

Please note that a few lines of the output have been deleted for better readability.

Step 1/3 : FROM ubuntu:16.04
 ---> b6f507652425
Step 2/3 : RUN apt-get update && apt-get install -y vim
 ---> Running in 8af22ce63f7c
Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [99.8 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Get:3 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [2051 kB]
Get:4 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [99.8 kB]
Get:5 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [97.4 kB]
Get:6 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB]
Get:7 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [15.9 kB]
Get:8 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [984 kB]
Get:9 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [8820 B]
...
Step 3/3 : CMD ["echo" , "Hello user"]
 ---> Running in ae98ecaafe74
Removing intermediate container ae98ecaafe74
 ---> 888983a0ad5c
Successfully built 888983a0ad5c
Successfully tagged test-cache:1.0

The docker image with the name test-cache:1.0 is successfully built. Now let us run the docker with the below command.

$ docker run test-cache:1.0

Output

Hello user

Now, let us see what happens if you build the docker image again?

$ docker build -t test-cache:1.0 -f Dockerfile .

Output

Sending build context to Docker daemon   12.8kB

Step 1/3 : FROM ubuntu:16.04
 ---> b6f507652425
Step 2/3 : RUN apt-get update && apt-get install -y vim
 ---> Using cache
 ---> 9ecd4b4e9da0
Step 3/3 : CMD ["echo" , "Hello user"]
 ---> Using cache
 ---> 888983a0ad5c
Successfully built 888983a0ad5c
Successfully tagged test-cache:1.0

You can observe that when you rebuild the same image without making any changes in the Dockerfile, it is using the cache of the previously built image. In case it identifies a change then it will rebuild the layers from that instruction onwards.

Here, the build process knows that there has been no change in the Dockerfile. This time the docker image build process was much faster than the first build.

This is the default behavior of docker and in case you want to override this and you want to build the image freshly, then use the no-cache option along with the docker build command.

 

Building with --no-cache

Syntax

$ docker build –no-cache -t <image-tag> -f Dockerfile .

When you execute the above command, the docker daemon will disregard the cache and won't use it and force a clean build of the docker image referring to the Dockerfile you specified while building it.

$ docker build --no-cache -t test-cache:1.0 -f Dockerfile .

Output

Please note that a few lines of the output have been deleted for better readability.

Sending build context to Docker daemon   12.8kB

Step 1/3 : FROM ubuntu:16.04
 ---> b6f507652425
Step 2/3 : RUN apt-get update && apt-get install -y vim
 ---> Running in 0c17d68b6014
Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [99.8 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Get:3 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [2051 kB]
Get:4 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [99.8 kB]
Get:5 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [97.4 kB]
Get:6 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB]
Get:7 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [15.9 kB]
Get:8 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [984 kB]

update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/view (view) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/ex (ex) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in auto mode
Processing triggers for libc-bin (2.23-0ubuntu11.3) ...
Removing intermediate container 0c17d68b6014
 ---> cbaa664269a6
Step 3/3 : CMD ["echo" , "Hello user"]
 ---> Running in 3828e3ecf4fe
Removing intermediate container 3828e3ecf4fe
 ---> eebce5a6bb5b
Successfully built eebce5a6bb5b
Successfully tagged test-cache:1.0

 

Conclusion

Docker has been one of the commonly used containerization tools and I hope this article helped you understand how to run the docker container without using a cache. This feature helps you build the docker image from scratch and lets all the latest updates be available in your application. Hope you enjoyed reading the article. In case you have any doubts, please feel free to add your questions in the comment section below and I shall try to respond at the earliest.

Happy learning!!

 

References

https://docs.docker.com/engine/reference/commandline/build/
https://stackoverflow.com/questions/35594987/how-to-force-docker-for-a-clean-build-of-an-image

 

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment