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
Linux Commands Cheat Sheet Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read
grep command in Unix/Linux The grep command in Unix/Linux is a powerful tool used for searching and manipulating text patterns within files. Its name is derived from the ed (editor) command g/re/p (globally search for a regular expression and print matching lines), which reflects its core functionality. grep is widely used by
7 min read
Linux/Unix Tutorial Linux is one of the most widely used open-source operating systems. It's fast, secure, stable, and powers everything from smartphones and servers to cloud platforms and IoT devices. Linux is especially popular among developers, system administrators, and DevOps professionals.Linux is:A Unix-like OS
10 min read
25 Basic Linux Commands For Beginners [2025] While performing a task, we all need shortcuts. Shortcuts help us to complete a task quickly. Linux comes with such commands which are one to two words, using that commands, you can perform several operations in no time. As a beginner, you must be aware of those basic Linux commands to complete an o
13 min read
Sed Command in Linux/Unix With Examples The SED command is one of the most powerful commands used during the process of text processing in Linux/Unix operating systems. The SED command is typically invoked for executing operations such as replace and search, text manipulation, and stream editing.With SED, you can manipulate text files wit
9 min read
AWK command in Unix/Linux with examples Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators. Awk is a utility that enables a programmer to write tiny but eff
8 min read
How to Find the Wi-Fi Password Using CMD in Windows Forgotten your Wi-Fi password? Need to connect a new device but canât recall the complex string of characters? You donât have to scramble for that sticky note or reset your router just yet! Hidden within Windows is a powerful, built-in tool that lets you retrieve your Wi-Fi password quickly and secu
8 min read
How to Find a File in Linux | Find Command The find command in Linux is used to search for files and directories based on name, type, size, date, or other conditions. It scans the specified directory and its sub directories to locate files matching the given criteria.find command uses are:Search based on modification time (e.g., files edited
9 min read
Introduction to Linux Shell and Shell Scripting If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understandi
8 min read
ZIP command in Linux with examples In Linux, the zip command compresses one or more files or directories into a single.zip archive file. This saves disk space, keeps data organized, and makes it simple to share or backup files. It's among the most used compression utilities, particularly when sharing large files via email or storing
6 min read