Open In App

What is Dockerfile?

Last Updated : 24 Jan, 2024
Like Article

The operating system (OS) libraries and dependencies required to run the application source code which is not reliant on the underlying operating system (OS) included in the Dockerfile, which is a standardized, executable component. Programmers may design, distribute, launch, run, upgrade, and manage containers using the open-source platform Docker. Enterprise Edition (EE) and Community Edition (CE) of Docker are both available. The Enterprise Version is for businesses and IT teams working on mission-critical production applications, while the Community Edition is suitable for small teams just learning Docker.



What is Docker?

Docker is an open-source platform where developers can containerize the application. Containers are accessible before Docker but have gained popularity as a result of Docker. The most crucial aspects of Docker are the Docker Engine and Docker Hub. The first one works on your local system to run your program, and the second one is similar to a cloud service where we can share our docker images with everyone.

What is Dockerfile?

The Dockerfile uses DSL (Domain Specific Language) and contains instructions for generating a Docker image. Dockerfile will define the processes to quickly produce an image. While creating your application, you should create a Dockerfile in order since the Docker daemon runs all of the instructions from top to bottom.

 Dockerfile is the source code of the image

What is Docker Image?

An artifact with several layers and a lightweight, compact stand-alone executable package that contains all of the components required to run a piece of software, including the code, a runtime, libraries, environment variables, and configuration files is called a Docker image.

What is Docker Container?

A container is a runtime instance of an image. Containers make development and deployment more efficient since they contain all the dependencies and parameters needed for the application it runs completely isolated from the host environment.

Dockerfile commands/Instructions


Represents the base image(OS), which is the command that is executed first before any other commands.


FROM <ImageName>

Example: The base image will be ubuntu:19.04 Operating System.

FROM ubuntu:19.04


The copy command is used to copy the file/folders to the image while building the image. 


COPY <Source> <Destination>

Example: Copying the .war file to the Tomcat webapps directory

COPY target/java-web-app.war  /usr/local/tomcat/webapps/java-web-app.war

3. ADD

While creating the image, we can download files from distant HTTP/HTTPS destinations using the ADD command.



Example: Try to download Jenkins using ADD command 


4. RUN

Scripts and commands are run with the RUN instruction. The execution of RUN commands or instructions will take place while you create an image on top of the prior layers (Image).


RUN < Command + ARGS>


RUN touch file

5. CMD

The main purpose of the CMD command is to start the process inside the container and it can be overridden.


CMD [command + args]

Example: Starting Jenkins 

CMD ["java","-jar", "Jenkins.war"]


A container that will function as an executable is configured by ENTRYPOINT. When you start the Docker container, a command or script called ENTRYPOINT is executed. It can’t be overridden.The only difference between CMD and ENTRYPOINT is CMD can be overridden and ENTRYPOINT can’t.


ENTRYPOINT [command + args]

Example: Executing the echo command.

ENTRYPOINT ["echo","Welcome to GFG"]


By using the MAINTAINER command we can identify the author/owner of the Dockerfile and we can set our own author/owner for the image.



Example: Setting the author for the image as a GFG author.


To know more the sytnx of Dokcerfile refer to the Syntax of Dockerfile.

Create a Dockerfile

  • Create a file named Dockerfile.
  • Add instructions in Dockerfile.
  • Build Dockerfile to create an image.
  • Run the image to create a container.

Example 1: Steps To Create Dockerfile With Example(Jenkins)

In this example, we will write the Dockerfile for Jenkins and build an image by using Dockerfile which has been written for Jenkins and we will run it as a container.

Step 1: Open Docker and create a file with the name Dockerfile.

Step 2: Open the Dockerfile by using the vi editor and start writing the command that is required to build the Jenkins image.

Edit Dokerfile


Dockerfile for Jenkins image

We used JDK as a base image because Jenkins’s pre-requisite is JDK after that we added a command called MAINTAINER which indicates the author or owner of the docker file and we added the ENV variable where we set the path for the Jenkins and by using RUN command we are creating the path and by using ADD we are downloading the Jenkins and starting the .war file with the help of CMD command.

FROM openjdk:11-jdk
LABEL env=production
ENV apparea /data/app
RUN mkdir -p $apparea
ADD $apparea
WORKDIR $apparea
CMD ["java","-jar","jenkins.war"]
Jenkins Dockerfile


Step 3: Build the image by using the below command with the help of Dockerfile and give the necessary tags. and the dot(.) represents the current directory which is a path for Dockerfile.

docker build -t jenkins:1 .
Docker image


Step 4: Run the container with the help image ID or tag of the image by using the below command.

docker run -d -p 8080:8080 <Imagetag/ID>
Docker Container


Step 5: Accesses the application (Jenkins) from the internet with the help of host port and hostIP (HostIP: Port)

containerized application

Example 2: Steps To Create Dockerfile 

Step 1: Create a file name called “Dockerfile”.By default when you run the docker build commands docker searches for a file named Dockerfile. However, it is not compulsory, you can also give some different names, and then you can tell the docker that this particular file is local but for now we will go with the Dockerfile.


Step 2:  The very first instruction that a docker file starts with is FROM. Here you have to give a base image. So for example, if you want to get a base image from Ubuntu we will use FROM Ubuntu.  

FROM ubuntu

Then the other instruction is you have to give a MAINTAINER. This is optional but it’s a best practice that you give the maintainer of this image so that it is very easy to find out who is the maintainer and you can give your name and email as well. And if you want you can just give the email as well without giving the name. But here we are giving the entire thing. 


Next, we want to run something so we will say run any command we can use RUN and add the command that you need to run.

RUN apt-get update

And if you want to run something on the command line during container creation you can give CMD and inside square brackets, and we add the command. Here it is as shown below:

CMD ["echo", "Hello Geeks!"]

At this point the file will have the following commands:

FROM ubuntu
RUN apt-get update
CMD ["echo", "Hello Geeks!"]

Step 3: Now we have to build the image so here are the commands you can use:

docker build /<FILE_LOCATION> 


docker build . -f Dockerfile.txt

It says docker build and you have to give the location of your docker file.  This will start building the image.

Bulding Image

Command to list the images 

docker image ls / docker images

List Of Images


In this post, we’ve covered a few of the crucial commands and instructions that are frequently used in Dockerfiles and detailed how Dockerfile works using examples. It provided easy-to-grasp syntax and examples for each and every Dockerfile command.

People Also Read

How Install Docker in Linux(Ubuntu)


Docker Commands


Docker Push


How to Create a Dockerfile in Node.js ?


Dockerfile – FAQs

What is Dockerfile Extension?

Dockerfile is the source code of docker image and basically there is no extension for Dockerfile or you may use “.dockerfile or .Dockerfile” they can be used but they are not mandatory. The standard file name is Dockerfile.

Dockerfile Copy Directory

The COPY command or instruction in the dockerfile is used to copy the directories from docker host to the docker image.

Dockerfile vs Docker Compose

  • Dockerfile: Dockerfile consists of all the steps that are required to create docker image. Dockerfile consists of base image and the all remaining environmental variables.
  • Docker Compose: YAML file consists of multi-container applications.Simplifies the orchestration and configuration of Dockerized applications by specifying services, networks, and volumes.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads