What is the difference between RUN and CMD in a Dockerfile?

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 CLIA text-based interface to run commands. (Command line interface) or APIA mechanism which supports communication between two software components. (Application programming interface).

Image build process

Whenever a build command is executed:

  1. Docker reads the Dockerfile

  2. Executes each command in order

  3. Creates a new layer to save each command's resulting states.

  4. Results in a Docker image which is used to create Docker containers.

Dockerfile Commands

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.

The RUN command

The 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 update
RUN 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.

The CMD command

CMD 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.

Sample Dockerfile

A sample Dockerfile is given below.

// Specify the official latest version, use as a parent image
FROM node:latest
// Set the working directory
WORKDIR /usr/src/app_name
// Copy content to the working directory
COPY package.json ./
// Update and install dependencies
RUN apt-get update
RUN npm install
// Specify port number
EXPOSE 8000
// Specify environment variables
ENV variable_name set_value
// Run app_name.js whenever the container is launched
CMD ["node", "app_name.js"]

Explanation

  • 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.

Summary

RUN and CMD commands when combined with other instructions are helpful in different use cases, but there are some key differences between the two.

Differences between RUN and CMD
Differences between RUN and CMD
Copyright ©2024 Educative, Inc. All rights reserved