Dockerfile: Understanding CMD and ENTRYPOINT Instructions.

Krupakar Reddy
3 min readJun 25, 2023

Greetings everyone!

Let us delve into the nuanced technicalities of the of the CMD and ENTRYPOINT instructions in a Dockerfile. As we understand the importance of effectively utilizing ENTRYPOINT and CMD in different scenarios. Allow me to impart my insights into their functionalities and optimal deployment strategies.

CMD Instruction:
The CMD instruction in a Dockerfile is used to specify the command that will be executed when the container starts. Let’s consider a basic Dockerfile example to illustrate this:

FROM ubuntu
CMD [“apt”, “-y”, “install”, “git”]

When we build an image from this Dockerfile and run the resulting container, it will install git. If we check the executed command of the container using docker ps -a, we will see it as apt -y install git.

It’s important to note that when running a container with a command, such as docker run testbox apt -y install httpd, the initial command (apt -y install git) will be replaced with the provided argument (apt -y install httpd). This can be verified by checking docker ps -a, where the executed command will be displayed as apt -y install httpd instead of git.

In summary, the CMD instruction allows us to replace or override the default command by passing arguments during container startup. If a Dockerfile contains multiple CMD instructions, only the last/latest one will be executed. Additionally, passing arguments during container startup will overwrite the existing command with the provided arguments, if no CMD instruction is present in the Dockerfile, a default command will be used when running the container..

For example:

FROM ubuntu
CMD [“echo”, “Hello Readers!”]
CMD [“echo”, “Hi Viewers!”]

In this case, when we run a container from this Dockerfile, it will output “Hi Viewers!”. However, if we pass an argument to overwrite the command, such as docker run demobox echo Hello Guys!, the output will be "Hello Guys!".

ENTRYPOINT:

The ENTRYPOINT instruction is used to configure the executable that should run after the container is initialized.

For example:

FROM ubuntu
ENTRYPOINT [“apt”, “-y”, “install”, “git”]

When we build an image from this Dockerfile and run the resulting container, it will install Git. If we pass an argument with the command, such as docker run demobox http, it will install both Git and Apache HTTP Server (httpd).

In this case, the argument (httpd) is appended after the initial command (git). To observe this, you can check docker ps -a, where the executed command will be displayed as apt -y install git httpd. Therefore, when using ENTRYPOINT, if we pass an argument, it will append and install it along with the existing command, rather than overwriting it like CMD.

It’s important to note that if there are multiple ENTRYPOINT instructions in a Dockerfile, only the last one will be executed. Similarly, if both CMD and ENTRYPOINT instructions are present in the Dockerfile, such as:

FROM ubuntu
ENTRYPOINT ["apt", "-y", "install"]
CMD ["git"]

In this case, when the container starts, it will by default install Git if no arguments are passed. However, if we pass arguments, they will overwrite the CMD instruction.

If both CMD and ENTRYPOINT instructions are present in the Dockerfile, CMD will be ignored if arguments are passed during container startup.

In summary, the main difference between CMD and ENTRYPOINT instructions is that CMD overwrites with passed arguments during container run, while ENTRYPOINT appends the arguments to the existing command.

Thank you for reading, and we appreciate your time and attention!

--

--