• Articles
  • Tutorials
  • Interview Questions

What is Dockerfile: Definition with Example

What is Dockerfile: Definition with Example

This blog will guide you through the fundamentals of Dockerfile, starting with an explanation of what Docker is and how to create a Dockerfile within Docker. We’ll cover the essential commands used in creating this Dockerfile, breaking down each component with practical examples to ensure a thorough understanding of the concept.

Table of Contents:

Kindly go through our explanation video in order to get a better understanding

What is Dockerfile?

Before we dive into Dockerfile, it’s important to understand Docker itself. Docker is a platform that simplifies the process of building, shipping, and running applications using containers. These containers are like lightweight, portable boxes that hold everything needed for software to work. Inside, you’ll find the code, tools, and settings—all neatly packed and ready to go. They are easy to move around and keep things separate, making sure everything runs smoothly.

A Dockerfile is a text file used to define the configuration of a Docker container. It contains a set of instructions that Docker uses to automatically create a container image. These instructions specify the base image, set up dependencies, copy files, configure environment variables, and define how the container should run. Dockerfiles simplifies the creation and replication of consistent container environments, enabling easy sharing and deployment of applications across different systems.

Want to know more about Docker in detail, enroll in Docker Certification Training Course!

Docker Image Overview

A Docker image is a self-contained package that includes all the dependencies an application needs to run. It resembles a virtual machine image but is lighter and more efficient. Docker images are created using a Dockerfile, which is a simple text file that contains instructions for building the image. 

Get 100% Hike!

Master Most in Demand Skills Now !

Common Commands in Dockerfile

  • From: This command sets the base image for your Docker image. It’s the starting point for the new image you’re creating. For example, using ‘FROM node:14‘ selects the base image as the latest version of Node available from the Docker Hub.
  • Run: The RUN command executes commands within the image during the build process. For instance, you might use ‘RUN apt-get update && apt-get install -y <package>’ to update package lists and install a package.
  • COPY: This command, commonly utilized to transfer application code or configuration files from the local filesystem into the image, facilitates the copying of files.
  • WORKDIR: This command sets the working directory for any subsequent instructions in the Dockerfile. It’s similar to ‘cd’ in the terminal. For instance, ‘WORKDIR /usr/src/app’ sets ‘/usr/src/app’ as the directory where subsequent commands or instructions within the Dockerfile will be executed.
  • EXPOSE:  EXPOSE doesn’t actually publish the port; rather, it serves as documentation for the ports that the container listens on. It informs Docker that the container listens on specific network ports at runtime. For instance, ‘EXPOSE 3000’ lets others know that the container listens on port 3000.
  • CMD: This command specifies what command to run when the container starts. It’s commonly used to preset default arguments for a running container. For instance, it can be used as follows: ‘CMD [“node”, “app.js”]‘, specifying the default command ‘node app.js’ for container startup.
  • ENV: This defines container-specific environment variables. These settings commonly customize the behavior or configurations of applications and services within the container. For example, ‘ENV NODE_ENV=production’ and ‘ENV PORT=3000’ set environment variables for the Node.js application.
  • USER: The USER command in a Dockerfile defines the user or user ID (UID) for the container, providing a way to specify the user context for the following instructions within the Dockerfile. For instance, you can use it as follows: ‘USER nodeuser’.

Overview of Docker Container

Docker serves as a software platform, empowering developers to encapsulate software into standardized entities known as containers. These containers are lightweight, portable, and self-contained, making them ideal for deploying applications across different computing environments. 

They operate in isolation from one another and the hosting system, ensuring consistent application performance irrespective of the underlying infrastructure. This isolation also enhances security and prevents conflicts between applications.

Want to excel in your interview? Refer to our Top Docker Interview Questions and Answers!

Creating and Building a Dockerfile

The development of a Dockerfile involves step-by-step instructions in a text file called Dockerfile, defining your container image’s configuration. Thus, each instruction specifies actions like setting the base image, installing dependencies, copying files, and exposing ports.

Let’s go through this process step by step to ensure you understand each command involved:

  • Step 1 – Use a text editor or command line to create a file named ‘Dockerfile’ in your project directory.
  • Step 2 –  In a Dockerfile, it’s important that the initial instruction defines the base image for your container.  Let’s understand the procedure using an example. Utilize the Node.js 14 image with Alpine Linux 3.16.
  • Step 3 – In the next step, use the ‘RUN’  instruction to install the necessary dependencies for your application. To install Node.js modules, use the following instructions:
  • Step 4 – Now we will use the ‘COPY ./ /app’ command to copy application files into the container image.
  • Step 5 – Use the WORKDIR instruction to configure the working directory for the container, providing the designated space where the application will be executed
  • Step 6 – Specify the ports your application will listen to using the ‘EXPOSE’ command. This enables external traffic to reach your application. For instance, let’s make port 3000 accessible here 
  • Step 7 – Next, use the CMD instruction to specify the command to be executed when the container starts, and to start a Node.js application, we will use
  • Step 8 – After finishing the Dockerfile, proceed to build the Docker image by executing the ‘docker build’ command. For instance, to create the image labeled as ‘my-app’ and tagged with ‘v1’
  • Step 9 – Once the image building is done, you can execute the container by utilizing the ‘docker run’ instruction. For instance, launching the ‘my-app:v1’ image and linking port 3000 from the host machine to port 3000


Dockerfiles act as a clear set of instructions, a roadmap guiding the effortless creation of container environments. These straightforward files hold a series of steps that shape Docker images into self-contained units. These images encapsulate applications and their needs, effortlessly adjusting to different computing environments. They help the developers by offering consistency and simplicity in deploying across diverse environments. Adopting Dockerfiles isn’t just about simplifying workflows; it marks a fundamental change in how we deploy software. It is a blend of reliability, scalability, and agility, reshaping how we deliver modern applications.

Still in doubt? Contact us at our Community Page!


How do I write a Dockerfile?

A Dockerfile consists of a series of instructions, each starting with a keyword, followed      by arguments and options. The instructions are executed in the order they appear in the file. Some of the most common Dockerfile instructions include:

  • FROM: Specifies the base image for the Docker image
  • RUN: Executes a command in the context of the Docker image
  • COPY: Copies files from the host machine to the Docker image
  • CMD: Specifies the default command to run when the Docker container is started.
  • EXPOSE: Specifies which ports to expose on the Docker container

How do I run a Dockerfile?

To run it, use the ‘docker build’ command mentioned earlier to build an image. After building the image, use ‘docker run’ followed by the image name to start a container from that image.

What is the entrypoint in a Dockerfile?

The ‘ENTRYPOINT’ instruction in a Dockerfile sets the default command that kicks in when the container begins. This instruction defines what the container will do by default. You can change this command temporarily when starting the container using ‘docker run’, but ‘ENTRYPOINT’ gives the container its usual behavior when it starts up.

What is the use of Dockerfile?

Dockerfiles serve as blueprints for building Docker images, providing a standardized and reproducible way to package and deploy applications in containers. They define the environment, dependencies, and configurations needed for running an application inside a Docker container.

What type of Dockerfile exists?

Dockerfiles don’t have specific types, but they can be customized for various purposes. For instance, there are base Dockerfiles for different programming languages or specific services, and you can create custom Dockerfiles depending on your application needs.

How do I delete a Docker image?

In order to delete a Docker image, it is suggested to use the ‘docker rmi’ command followed by the image ID or image name. For example, type ‘docker rmi <image_id>’ or ‘docker rmi <image_name>’. Ensure no containers are currently running based on the image you wish to delete, as Docker won’t allow the deletion of images that are in use.

Course Schedule

Name Date Details
AWS Certification 22 Jun 2024(Sat-Sun) Weekend Batch
View Details
AWS Certification 29 Jun 2024(Sat-Sun) Weekend Batch
View Details
AWS Certification 06 Jul 2024(Sat-Sun) Weekend Batch
View Details

About the Author

Application Architect

Rupinder is a certified IT expert in AWS and Azure, working as a DevOps Architect and specializing in cloud and infrastructure. He designs and builds entire IT setups for important apps in banking, insurance, and finance.