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
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
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
Copying Files to and from Docker Containers While working on a Docker project, you might require copying files to and from Docker Containers and your Local Machine. Once you have built the Docker Image with a particular Docker build context, building it again and again just to add small files or folders inside the Container might be expensive
9 min read
How To Use Docker Desktop To Deploy Docker Containerized Applications Docker Desktop is an user-friendly tool that simplifies the deployment of Docker containerized applications on your local machine. It provides a graphical interface for managing containers, images, and volumes, making it accessible for developers and DevOps professionals. In this guide, we will walk
9 min read