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

Introduction

Docker has been one of the best software platforms and a lot of industries are dockerizing their application which helps software developers to build, test and deploy their applications in many environments seamlessly.

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.

Advertisement

Let us get started!

As you all know the docker build command refers to building the docker images from a Dockerfile which also includes files located in the path specified during the building of the Docker image. This is called a context in terms of Docker.

In general, the docker image build process should be faster and more efficient. Every instruction given in the Dockerfile when executed, build an image layer on top of other and hence layers that were built previously will be cached so that they can be reused. This in turn will help you build the docker images faster since you are not building the image from scratch and you are using the cache.

 

Why use docker build --no-cache?

Sometimes you don't want to use the cache and you need to build the image from scratch every time. This can help you in building the docker images which have all the latest updates available in your application.

In that case, Docker provides an option to build the image without using a cache i.e. “--no-cache”. Let us see how this works with an example.

 

How docker build works without --no-cache?

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

Advertisement
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.

 

Using docker build --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.

Advertisement
$ 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

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

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 either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X