Difference between RUN vs CMD vs ENTRYPOINT Docker Commands
Last Updated :
18 Jun, 2024
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:
Input fileIf we use additional arguments along with the docker run command such as "bash", it will simple open the bash and not echo anything.
Output3. 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
Input The output of the above commands on running the Docker Container without any additional arguments would be -
Geeksforgeeks Docker Tutorials
OutputIn 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.
Similar Reads
Running Commands Inside Docker Container
If you are working on an application inside the Docker Container, you might need commands to install packages or access file system inside the Docker Container. Executing commands inside Docker Containers should be easy enough for you since you have to do it multiple times across your development ph
6 min read
Difference Between #!/usr/bin/bash and #!/usr/bin/env bash
In this article, we are going to see that what is the difference between #!/usr/bin/bash and #!/usr/bin/env bash What is #!? This is called the shebang character sequence consisting of the character's number sign and an exclamation mark at the beginning of the script. It is also called sharp-exclama
2 min read
Docker Run Command - Complete Tutorial For Beginners
Docker launches the containers in seconds, and the heart of running containerized applications lies in the powerful command known as 'docker run'. The 'docker run' is used to create a running container from using a docker image. It is used with options, docker images, commands, and arguments. It is
15+ min read
Running Docker Containers as Non-Root User
By default, Docker Containers run as Root Users. Now, if you are running applications inside Docker Containers, you have access to all the root privileges. This poses a great security threat when you deploy applications on large scale inside Docker Containers. Because if somehow your application get
2 min read
Bash Script - Difference between Bash Script and Shell Script
In computer programming, a script is defined as a sequence of instructions that is executed by another program. A shell is a command-line interpreter of Linux which provides an interface between the user and the kernel system and executes a sequence of instructions called commands. A shell is capabl
4 min read
Docker Compose - How To Execute Multiple Commands?
Docker Compose is an orchestration tool that comes with Docker and enables users to run and manage multi-container applications using a YAML format file. Docker Compose comes pre-installed with Docker and does not require any additional installation or activation. In this article, we will explore wh
7 min read
How To Create a Docker Container from an Existing Image?
Docker is an open-source software, that is used to contanerize our applications. Containerizing applications makes deployment a lot easier. For containerizing applications, docker uses Docker images, which act like templates for making containers. Today we will learn how to create a container from a
9 min read
Top 50+ Docker Interview Questions and Answers (2024)
Docker is an open-source platform that simplifies the deployment, scaling, and management of applications using lightweight containers. It has transformed the way apps are built, shipped, and deployed, becoming a key tool for many top companies like Uber, Airbnb, Google, Netflix, and Amazon. Docker
15+ min read
How To Fix "Bash: Docker: Command Not Found" In Linux
Docker has become an essential tool for developers and system administrators to manage and deploy applications efficiently. However, encountering the error message "Bash: Docker: Command Not Found" can be frustrating, especially when you're trying to work with Docker containers. Here, we'll explore
4 min read
How to Install and Configure Docker in Ubuntu?
Docker is a platform and service-based product that uses OS-level virtualization to deliver software in packages known as containers. Containers are separated from one another and bundle their software, libraries, and configuration files. Docker is written in the Go language. Docker can be installed
6 min read