Understanding CMD and ENTRYPOINT Differences in Docker

Table of contents

What are CMD & ENTRYPOINT?

CMD and ENTRYPOINT are Dockerfile instructions used to define the behavior of a container when it starts. Both are commonly used to specify what command should run inside the container, but they serve different purposes..

Why do developers get confused between CMD and ENTRYPOINT?

Because both CMD and ENTRYPOINT appear to define startup behavior in a Docker container, they can look interchangeable at first. This often leads to confusion, especially for beginners or those without hands-on Docker experience.

This blog explains the differences clearly, with real-world examples, to help you build a strong foundation.

What are the two forms of CMD and ENTRYPOINT?

Both CMD and ENTRYPOINT support two syntax styles:

Form Type Syntax Example Description
Shell Form CMD echo "Hello World" Runs in a shell (/bin/sh -c)
Exec Form CMD ["echo", "Hello World"] Direct exec call, preferred for arguments

Docker CMD

CMD in Dockerfile defines the default executable of a Docker image. You can run this image as the base of a container without adding command-line arguments. In that case, the container runs the process specified by the CMD command.

The CMD instructions are only utilized if there is no argument added to the run command when starting a container. Therefore, if you add an argument to the command, you override the CMD command specified.

To demonstrate how CMD works, we will create a sample container with CMD instructions. Here's a sample Dockerfile using Ubuntu as a base image and a basic CMD instruction.

FROM ubuntu
CMD ["echo", "Hello World"]

Now, let’s build an image out of this Dockerfile and run a container from the image created. To build an image with Dockerfile, use the following command:

docker build -t cmd-instructions .

To run a container out of the image build, execute the following command:

docker run cmd-instructions
cmd-demo

Once you run the container, it prints Hello World and exists as you can see in the above image. This is so because no command line arguments were passed and hence default CMD instructions were executed. However, if you add an argument while starting a container, it overrides the CMD instructions given in Dockerfile. To override the default CMD instructions, execute the following commands:

docker run cmd-instructions echo "message changed"

docker run cmd-instructions printenv
cmd-override

As you can see in the above image, when we passed parameters with docker run a command, the default CMD instructions were overridden and it gives the output as the value of commands passed as arguments.

Note: If multiple CMD instructions are passed in a Dockerfile, all execpt the last instruction will be ignored.

Docker ENTRYPOINT

ENTRYPOINT is another instruction used to specify the behavior of the container once started. Just like with CMD, we need to specify a command and parameters. However, in the case of ENTRYPOINT we cannot override the ENTRYPOINT instructions by adding command-line parameters to the docker run command. By opting for this instruction method, you imply that the container is specifically built for certain use cases where the command should not be overridden.

Let's start the hands-on. We will again create a sample Dockerfile using Ubuntu as a base image, but this time providing an ENTRYPOINT command instead in Dockerfile as mentioned below.

FROM ubuntu
ENTRYPOINT ["echo", "Hello World"]

Now again, let’s build an image out of this Dockerfile and run a container from the image. Execute the below command to build an image and run the container from the docker image.

docker build -t entrypoint-instructions 
docker run entrypoint-instructions

The above command will print the ENTRYPOINT instructions mentioned in Dockerfile as you can see in the below image. Now let's try to pass some command line arguments and see if it overrides the default instructions or not.

entrypoint-demo

As you can see in the above image, docker run did not override the initial instruction but took our argument i.e, printenv command as a parameter for echo instruction.

Note: The default nature of ENTRYPOINT in Dockerfile is not to be overridden by parameters passed in command, but with some explicit docker flags, it can be overriden. Let's look into it.

If you want to override the ENTRYPOINT instructions, execute the following command.

docker run --entrypoint printenv entrypoint-instructions
ENTRYPOINT default instructions override
Note: The command passed in --entrypoint argument is temporary and once your exit out of the container and run the container again, it would take the default ENTRYPOINT instructions if --entrypoint is not passed explicitly.


Using ENTRYPOINT & CMD Together

There are fundamental differences between ENTRYPOINT and CMD but there can be many situations in which combining both would be the best solution for the Docker container. In such cases, the executable is defined with ENTRYPOINT, while CMD specifies the default parameter.

Note: If you are using both instructions, make sure to keep them in exec form.

Let's create a new Dockerfile with both the parameters stated. Here's a sample Dockerfile with both instructions.

FROM ubuntu
ENTRYPOINT ["echo", "Hello"]
CMD ["Abhinav"]

Now, let's build the image and run the container using docker run. The expected output should be the command passed in ENTRYPOINT followed by CMD instructions.

docker build -t entrypoint-cmd .
docker run entrypoint-cmd
CMD & ENTRYPOINT working together

As observed in the above image, the output of the command is default ENTRYPOINT instructions followed by CMD those given in the Dockerfile. Now let's try to pass some parameters and see the changes.

docker run entrypoint-cmd @abhinavd26
CMD & ENTRYPOINT with parameters

As you can see the ENTRYPOINT took "echo" as instruction and CMD as a parameter that's why the parameter that we passed got overridden in CMD and later on ENTRYPOINT took it as a parameter.

Use Cases

Prefer ENTRYPOINT to CMD when building executable Docker images, and you need a command always to be executed. Additionally, use CMD if you need to provide extra default arguments that could be overwritten from the command line when the docker container runs. It is not necessary to use both commands together, try to find your use case while building the Dockerfile and select the appropriate command.

After you have built the Docker containers, you may want to orchestrate them using Kubernetes. Within Kubernetes, there is a different mechanism of defining the Entrypoint and CMDs for the contianer running inside the pod.

Kubernetes uses something called as Commands and Arguments which are defined in the container spec in the configruation files. To learn about Commands and Arguments in Kubernetes, please check out this detailed blog.

visibility-guide

Conclusion

Hurray! You made it to the end hope you got a better understanding of how ENTRYPOINT and CMD works and when to use them in your own projects. Explore and try out this with different scenarios and feel free to join our discord community. We would be happy to help you.

Related articles

Related articles