Open In App

Difference between RUN vs CMD vs ENTRYPOINT Docker Commands

Last Updated : 18 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Understanding the distinctions between the RUN, CMD, and ENTRYPOINT commands in Docker is essential for optimizing your containerized applications. Each of these commands serves a unique purpose in Dockerfile instructions, impacting how containers are built and executed.

In this article, we will delve into the specifics of RUN, CMD, and ENTRYPOINT, exploring their roles, differences, and best use cases to help you master Dockerfile configurations.

But before we dive into the explanation, we need to first understand the different execution forms. We can use two different forms for executing commands in Docker.

Shell Form:

 Normal shell processing takes place if we opt for shell form execution of commands. Behind the scenes, the bash calls the /bin/sh -c. The general form of shell commands is as shown below:

<Instruction> <Command>

 To get a clearer picture, look at the commands below.

RUN apt-get -y install firefox
CMD echo "GeeksforGeeks"
ENTRYPOINT echo "GeeksforGeeks"

Both the above commands outputs "GeeksforGeeks". The shell form of execution commands is generally used for RUN commands.

Executable Form:

Executable form of commands is generally used for CMD and ENTRYPOINT commands. The general form of executable commands is as shown below:

<Instruction> ["executable", "parameter no. 1", "parameter no. 2", ...]

Using the executable form of commands executes the commands directly and shell processing does not take place. Check out the commands below.:

ENTRYPOINT ["/bin/echo", "geeksforgeeks"]
CMD ["/bin/echo", "geeksforgeeks"]

Let's now try to understand the RUN, CMD, and ENTRYPOINT commands in-depth.

1. RUN command:

When you use a RUN command in your dockerfile, it always creates a new intermediate image layer on top of the previous ones. That's why it is always recommended chaining all the RUN commands together.

RUN command in executable form is:

RUN ["apt-get", "install", "firefox"]

RUN command in shell form is :

RUN apt-get -y install firefox

2. CMD command

A CMD command is used to set a default command that gets executed once you run the Docker Container. In case you provide a command with the Docker run command, the CMD arguments get ignored from the dockerfile. In the case of multiple CMD commands, only the last one gets executed.

CMD ["python3", "app.py"]

If you are using an ENTRYPOINT in your dockerfile, you can add some additional parameters using the CMD command's following form.

CMD ["parameter 1", "parameter 2"]

Note that the CMD commands get ignored if you provide arguments in your Docker run command.

sudo docker run -it ubuntu bash

If you use the above command and at the same time, you have used a CMD command in your dockerfile, it gets ignored and simply opens the bash.

For example, if the dockerfile contains:

dockerfile
Input file

If we use additional arguments along with the docker run command such as "bash", it will simple open the bash and not echo anything.

output
Output

3. ENTRYPOINT command

An ENTRYPOINT command, unlike CMD, does not ignore additional parameters that you specify in your Docker run command.

Consider the example below:

ENTRYPOINT ["echo", "Geeksforgeeks "]
CMD ["Docker Tutorials"]

For example, if the dockerfile is

dockerfile
Input 

The output of the above commands on running the Docker Container without any additional arguments would be - 

Geeksforgeeks Docker Tutorials
output
Output

In case you specify additional parameters, the CMD arguments get ignored.

To conclude, understanding the differences between the RUN, CMD, and ENTRYPOINT commands is crucial for effective Dockerfile management. RUN is used to build the image and install software, CMD provides default commands for container execution, and ENTRYPOINT sets the main command for the container. Mastering these commands allows you to create more efficient, flexible, and predictable Docker containers.


Next Article

Similar Reads