RUN vs CMD vs Entrypoint in Docker

The commands RUN, CMD, and ENTRYPOINT usually cause a lot of confusion among Docker developers. Understanding all three commands conceptually will help to have a clearer understanding of Docker image building and container execution.

When we try to build an image using a Dockerfile, the instructions are executed step by step. The first instruction is usually pulling a base image such as an OS distribution like Ubuntu or CentOS. After that, we modify the base image either by including more images using FROM and AS commands or by modifying the images. Every such instruction creates a new intermediate image build and for each of them, caches are created. The final Docker image can be considered as a layered structure where there is a core or a base image and on top of that, there are several layered intermediate images.

To understand all these three commands in depth, we first need to understand what are shell and exec forms.

Exec Form

This form is generally used for ENTRYPOINT and CMD commands. It is of the form −

<instruction> ["executable", "parameter", "parameter", ....]

When we execute an instruction in such a form, shell processing does not happen and the executable is called directly.

Consider the commands below −

RUN ["apt-get", "install", "vim"]
ENTRYPOINT ["/bin/echo", "TutorialsPoint"]
CMD ["/bin/echo", "TutorialsPoint"]

If we write a Dockerfile with the following instructions −

ENV variable TutorialsPoint
ENTRYPOINT ["/bin/echo", "$variable"]

When we try to run the image, the following output is generated −

$variable

Shell Form

When we try to execute an instruction using the shell form, normal shell processing takes place. It calls the command /bin/sh -c {command} behind the scenes.

It is of the form − <Your instruction> <the command>

Consider the commands below −

RUN apt-get -y update
CMD echo "TutorialsPoint"
ENTRYPOINT echo "TutorialsPoint"

The following Dockerfile −

ENV variable TutorialsPoint
ENTRYPOINT echo "$variable"

will output −

TutorialsPoint

Now that we have understood the two forms in-depth, let us continue with the three commands.

RUN Command

The RUN command always gets executed in a new layer during the image build process. It allows you to install packages and applications on top of an existing image layer and creates a new layer on top of it.

It can be written in both shell and exec forms. Examples include −

RUN apt-get -y update                     # shell form
RUN ["apt-get", "install", "vim"]         # exec form

CMD Command

Using a CMD command, you will be able to set a default command. This will be executed if you run a particular container without specifying some command. In case you specify a command while running a Docker container, the default one will be ignored. Note that if you specify more than one CMD instruction in your Dockerfile, only the last one will be executed.

Following are the different ways through which you can execute a CMD command −

  • CMD <command> parameter1, parameter2 .... (Shell form)

  • CMD ["executable command", "parameter1", "parameter2", ....] (Exec form)

  • CMD ["parameter1", "parameter2"] (Parameters to ENTRYPOINT)

The third way is used to set some additional default parameters that will be inserted after the default parameters when you are using an ENTRYPOINT in exec form and if you run the container without specifying any arguments in the command line.

Consider the command below inside a Dockerfile −

CMD echo "TutorialsPoint"

The output if you run it without specifying arguments (docker run -it image_name) will be −

TutorialsPoint

If you run it by specifying CLI arguments (docker run -it image_name /bin/bash), it will simply open a bash shell.

ENTRYPOINT Command

It looks similar to a CMD command. However, the difference is that it does not ignore the parameters when you run a container with CLI parameters. The two forms of ENTRYPOINT command are −

  • ENTRYPOINT <command> parameter1, parameter2, ... (Shell form)

  • ENTRYPOINT ["executable command", "parameter1", "parameter2", ....] (Exec form)

When you use an exec form of ENTRYPOINT command, it will allow you to set additional parameters using CMD command. If you use it in shell form, it will ignore CMD parameters or any CLI arguments.

Consider the example below −

ENTRYPOINT ["/bin/echo", "TutorialsPoint"]
CMD ["Docker"]
  • If you try to run the container without specifying any CLI arguments (docker run -it image_name), output will be − TutorialsPoint Docker

  • If you specify CLI arguments (docker run -it image_name DockerTutorials), output will be − TutorialsPoint DockerTutorials

Comparison

Command Execution Time Purpose CLI Override
RUN Image build time Install packages, create layers Not applicable
CMD Container runtime Default command for container Completely overridden
ENTRYPOINT Container runtime Fixed command with flexible params Parameters appended

Conclusion

If you want to specify default arguments and want them to be overwritten when specifying CLI arguments, use CMD commands. If you want to run a container with the condition that a particular command is always executed, use ENTRYPOINT. RUN is simply used to build additional image layers over the base image during the build process.

Updated on: 2026-03-17T09:01:38+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements