A Dockerfile is simply a text file, serving as a script, that contains all the commands required to build a Docker image.
A Docker image is an executable software package that contains libraries, code, configuration files, and environment variables required to run an application.
These images are used to create Docker containers, simply an application’s runtime instances. It is created whenever you run a Docker image.
Docker container supports move, delete, stop, and start operations using Docker
Whenever a build command is executed:
Docker reads the Dockerfile
Executes each command in order
Creates a new layer to save each command's resulting states.
Results in a Docker image which is used to create Docker containers.
The Dockerfile contains several commands, each performing a specific operation during the image build process. Some common commands include MAINTAINER
, CMD
, RUN
, COPY
, FROM
, ENV
, EXPOSE
, and ADD
.
In this Answer, we will discuss the difference between RUN
and CMD
.
RUN
commandThe RUN command executes commands during the build process of the Docker image. Whenever a RUN command is executed, it creates a new layer in the Docker image.
It is mainly used to:
Set up environment variables
Install software packages
Perform any required configurations
Example
RUN apt-get updateRUN npm install
Whenever you build an image, the RUN
instruction will get executed.
Here,
Line 1: Updates the package list for upgrades, and saves the result of this update as a new layer for the Docker image.
Line 2: Install all the required packages specified in the package.json
file of the project. It saves the result as a layer on top of the previous one.
CMD
commandCMD
command contains defaults to execute the Docker container upon its initiation. It includes an executable which means that you specify the program to be run whenever you start a Docker container. It is specified at the end of a Dockerfile.
Note: Only the last command will be executed if there are more than one
CMD
commands.
Example
CMD ["node", "app_name.js"]
Here,
Line 1: If you are running a Node application, the executable would be node
and app_name.js
would be the name of your application.
Docker will execute node app_name.js
which starts the Node.js
application.
A sample Dockerfile is given below.
// Specify the official latest version, use as a parent imageFROM node:latest// Set the working directoryWORKDIR /usr/src/app_name// Copy content to the working directoryCOPY package.json ./// Update and install dependenciesRUN apt-get updateRUN npm install// Specify port numberEXPOSE 8000// Specify environment variablesENV variable_name set_value// Run app_name.js whenever the container is launchedCMD ["node", "app_name.js"]
Line 2: It sets the Docker base image for the container to the latest Node.js version.
Line 5: This line sets the working directory in the Docker container to /usr/src/app_name
. All the following RUN
and CMD
commands will operate in the specified directory.
Line 8: This instruction copies the package.json
file from your local directory into the current working directory in the Docker container.
Line 11: It updates the package list for upgrades, and saves the result of this update as a new layer for the Docker image.
Line 12: The specified instruction installs all the required packages specified in the package.json
file of the project. It saves the result as a layer on top of the previous one.
Line 15: The EXPOSE
directive informs the Docker that the container listens on the specified port number at the runtime.
Line 18: The ENV
directive sets the environment variable variable_name
to the value set_value
.
Line 21: The CMD
directive provides default to execute a container. If you are running a Node application, the executable would be node
and app_name.js
would be the name of your application.
RUN
and CMD
commands when combined with other instructions are helpful in different use cases, but there are some key differences between the two.