Difference between RUN vs CMD vs ENTRYPOINT Docker Commands
Commands such as CMD, RUN and ENTRYPOINT are interchangeably used when you are writing a dockerfile to create the Docker Image. However, if you have just started using Docker or you don’t have enough hands-on experience working with these commands, then these commands might cause a lot of confusion for you. In this article, we are going to discuss all three commands in-depth with practical examples.
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:
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 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 :
If we use additional arguments along with the docker run command such as “bash”, it will simple open the bash and not echo anything.
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
The output of the above commands on running the Docker Container without any additional arguments would be –
Geeksforgeeks Docker Tutorials
In case you specify additional parameters, the CMD arguments get ignored.
To conclude, in this article, we discussed the shell and executable form for executing dockerfile instructions. We then discussed the differences between RUN, CMD and ENTRYPOINT commands each with an example.