Simplifying Dockerfile Commands

Mahesh KumarMahesh Kumar
3 min read

We have all encountered a problem in writing the Dockerfile commands for our application, in this blog I will be explaining each Dockerfile’s commands in a easiest way possible, so that you can choose which command to use in a appropriate way.

What is Docker?

Docker is similar to Virtual Machine, which solves the classic software development problem “It works on my Machine” . It generally converts your applications into an Docker Image, which contains the information of dependencies and instructions that will be used when the application is deployed in an other machine.

How to create a Docker Image?

Prerequisites

You need to be familiar with how docker works, and basic definition of images and containers. You can watch a video about docker here.

To create a docker image, you need to create a file named Dockerfile. A Dockerfile provides instructions to the image builder on the commands to run, files to copy, startup command, and more.
.dockerignore file is similar to .gitignore, where you can add files which is irrelevant, or heavy.

A typical Dockerfile looks something like this, do not get overwhelmed by the code, I will break down each commands in simplest way possible.

hello

FROM

In general programming language you might have imported any external libraries, to add a better functionality to your application. In the same way, the FROM command specifies the base image that the build will extend upon.
The base images comes with pre-installed necessary runtimes (Node or Python), so you don’t have to manually set everything up. It’s like importing a ready-made environment for your app.
You can find the information about the base images and it’s version in Docker Hub.

FROM <image_name>:<image_version>

# example
FROM node:24-alpine

WORKDIR

The workdir command sets the working directory for any instructions that come after in Dockerfile. Think it like, In your terminal, when you run “cd my-folder”, you're changing the current directory. Similarly, in Docker, WORKDIR tells the container where it should "be" when executing future commands.

WORKDIR /path/to/folder

# example
WORKDIR /app

COPY

The COPY instruction copies new files or directories from source/local machine and adds them to the filesystem of the image at the path destination.

COPY <source-path> <destination-path>

# example
COPY . /app
# This copies everything from the current directory into the /app directory inside the image.

RUN

The RUN command, generally executes the command attached to it. Commands like installing dependencies that is necessary to build before starting an application. The machine will wait till the run command is completely executed, and then process remaining instruction in the Dockerfile.

RUN <command>

# example
RUN npm install

CMD

Lastly, you need to specify how to execute/run the application, it is the same command which you use to run the application locally. You should break the words, and enclose it store it as array of strings.

CMD [ "<command>", "<argument_1>", "<argument_2>" ]

# example
CMD [ "node", "server.js" ]

These are the main commands that you need to know while building a Dockerfile, there is also many other commands which are used in a specific cases.
Hope you enjoyed reading this blog, make sure to give a visit to my site.

1
Subscribe to my newsletter

Read articles from Mahesh Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mahesh Kumar
Mahesh Kumar