About Dockerfile

About Dockerfile
Written by Nilima PaulFebruary 12, 2022
11 min read
Nilima Paul

Technology Security Analyst

We will know in this article, what's the About Dockerfile.

Introduction to Dockerfile:

Dockerfile is utilized to construct Docker Images. It is a basic text document that comprises a bunch of directions or orders that is executed by a mechanized form process in strides from start to finish. It is utilized to make our own Docker picture and for the most part, we use a parent Docker picture to construct our own Docker picture notwithstanding, we can make a base picture also. The name of the Dockerfile should be 'Dockerfile' else Docker daemon will toss a mistake and it is case touchy. We utilize the 'docker assemble' order to make a Docker picture from a Dockerfile.

How Dockerfile works in Docker:

Given below shows how Dockerfile works in Docker with an example.


FROM ubuntu
ENV APP nginx
RUN apt-get update && apt-get install -y APP
WORKDIR /var/www/html/
ADD index.html ./
CMD ["nginx", "-g", "daemon off;"]

Let’s see each instruction of Dockerfile mentioned in the above sample Dockerfile:

1. FROM:

More often than not we utilize an authority picture on which we will construct our Docker picture with the goal that it is the foundation of our Docker picture. To make a base picture, we use 'without any preparation' in the Dockerfile. In the above Dockerfile, 'ubuntu' is utilized as a base picture, which is called parent picture. Other guidance referenced in the Dockerfile will change this Docker picture.

2. ENV:

It is utilized to set the climate variable while making a Docker picture. It has key-esteem sets and we can set various factors utilizing "<key>=<value>". If we don't utilize the equivalent (=) sign then it permits a solitary variable as displayed in the above Dockerfile.


ENV key1=”value1” key2=”value2” \
ENV key1 value1
ENV key2 value2
ENV key3 value3

3. RUN:

RUN guidance is utilized to execute shell order in the new layer and submit the outcome in another Docker picture. We can run separate long or complex RUN guidance in various lines utilizing an oblique punctuation line as beneath:


RUN apt-get update && apt-get install –y \
package1 \
package2 \


It changes the current working catalog in the Docker picture and that can be used by additional guidance. In the above model, the working registry has been changed to "/var/www/HTML" and afterward in the following guide we use '.' to indicate that add the record 'index.html' to the functioning catalog that is "/var/www/HTML".

5. ADD:

It is utilized to add records from the neighborhood host to the Docker picture. Duplicate guidance has comparative usefulness nonetheless, ADD has a few different highlights also like tar extraction and far off URL support. In the above Dockerfile, the index.html document from localhost has been added to the current working registry of the Docker picture.


Uncover guidance tells about the port on which a compartment tunes in for associations. It doesn't uncover the port while building the picture. It gives overall data about the port assuming we run a compartment utilizing this picture. In the above model, the holder will tune in on port 80 assuming it is made utilizing the picture that is worked by the above Dockerfile and that should be uncovered while making a compartment to get to it remotely.

7. CMD:

It characterizes what might get executed when a compartment gets made utilizing the Docker picture or it gives the default to an executing holder. We ought to characterize just a single CMD in a Dockerfile, but on the off chance that we characterize different CMD guidelines in a Dockerfile, just the last CMD will get executed. Likewise assuming we determine any order while running the compartment, the predetermined order will come first and CMD guidance won't get executed.

We can utilize ENTRYPOINT guidance that permits making an executable compartment. It has two structures, one is an 'executive' and another is a 'shell' structure. Assuming that we pass order line contention while running the holder utilizing the 'docker run <image>' order, the determined order gets added after all components in an executive structure ENTRYPOINT. We utilize the banner '- entry point to supersede the ENTRYPOINT guidance while running the compartment utilizing the 'docker run <image>' order.

How about we fabricate the Docker picture utilizing above example Dockerfile utilizing underneath order:


$docker build -t <image_name>:<image_tag><path_of_Dockerfile>



$docker build -t my_nginx.


  • In the above snapshot, it is clear that the build has a total of 7 steps and it is equal to the number of instructions specified in Dockerfile which means each instruction is getting executed one by one.

Step 1: Docker daemon searches for the image mentioned in the FROM instruction i.e. ubuntu, if the image is not available locally it downloads from the hub, in the above case ubuntu already exists locally.

Step 2: Set environment variable APP to Nginx.

Step 3: Updates the OS and install Nginx.

Step 4: Changes the working directory to ‘/var/www/home.

Step 5: Copy local ‘index.html’ file to Docker image to ‘/var/www/home.

Step 6: It shows PORT 80 needs to be exposed to access it externally.

Step 7: This is the last step that sets the default command that runs the Nginx daemon as a background job when any container gets created using this Docker image.

Advantages of Dockerfile:

Given below are the advantages :

  • It saves our time via robotizing every one of the means with sets of guidelines that get executed during the form so we don't need to physically roll out the improvements to the holder and submit it.
  • We can keep Dockerfiles on any source control the executive's instruments like git, svn, and so forth and exploit presented by those devices like form control, spreading, and so on
  • We can share Dockerfile effectively with different partners or gatherings regardless of whether we have a focal vault for pictures.
  • Every guidance in Dockerfile makes a layer in the Docker picture that is valuable to get how the picture is treating the scene by actually looking at the historical backdrop of the picture. It likewise helps in the quicker picture working by utilizing reserve.


We can utilize 'docker submit' after rolling out expected improvements to the compartment to make a Docker picture also yet it isn't prescribed as we miss to take the benefits referenced above with regards to Dockerfile. There are a lot more orders accessible to use in Dockerfile.

Docker swarm
Was this blog helpful?
You must be Logged in to comment
Code Block

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance