Whenever we are working on Docker, people often get confused between the commands like
ENTRYPOINT which are mostly used while writing a Dockerfile. However, if you are a beginner and just started using Docker or you don’t have enough hands-on experience, this blog will help you to understand these commands in-depth with hands-on examples.
Before we begin, it is important to discuss the forms of instructions. Docker
CMD can have two forms i.e,
<instruction> <command> ---> shell form
<instruction> ["executable", "parameter"] ---> exec form
- CMD echo "Hello World" (shell form)
- CMD ["echo", "Hello World"] (exec form)
- ENTRYPOINT echo "Hello World" (shell form)
- ENTRYPOINT ["echo", "Hello World"] (exec form)
What are CMD & ENTRYPOINT?
Before moving ahead with the differences and some hands-on, let's try to understand what are these and why we need them. Both the parameters are used to specify the programs/commands to execute while initializing a container from docker image.
CMD: Sets default parameters that can be overridden from the Docker command line interface (CLI) while running a docker container.
ENTRYPOINT: Sets default parameters that cannot be overridden while executing Docker containers with CLI parameters.
Different between CMD & ENTRYPOINT
As we discussed above, these are used to specify programs that execute when a container starts, but the major differences between the two are:
CMD commands are ignored by daemon when there are parameters stated within the
docker run command while
ENTRYPOINT instructions are not ignored but instead are appended as command line parameters by treating those as arguments of the command.
Now, let’s take a closer look. We’ll use both command forms to go through the different stages of running a Docker container.
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 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
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
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 instruction 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
As you can see in the above image, when we passed parameters with
docker run command, the default
CMD instructions were overridden and it gives the output as value of commands passed as arguments.
Note: If multiple
CMDinstructions are passed in a Dockerfile, all execpt the last instruction will be ignored.
ENTRYPOINT is the another instruction used to specify the behaviour of 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 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 below image. Now let's try to pass some command line arguments and see if it overrides the default instructions or not.
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
ENTRYPOINTin 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
Note: The command passed in
--entrypointargument is temporary and once your exit out of the container and run the container again, it would take the default
--entrypointis not passed explicitly.
Using ENTRYPOINT & CMD Together
There are fundamental differences between
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
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 the 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 follwoed by
docker build -t entrypoint-cmd . docker run entrypoint-cmd
As observed in the above image, the output of the command is default
ENTRYPOINT instrutions followed by
CMD given in Dockerfile. Now let's try to pass some parameters and see the changes.
docker run entrypoint-cmd @abhinavd26
As you can see the
"echo" as instruction and
CMD as a parameter that's why the parameter which we passed got overridden in
CMD and later on
ENTRYPOINT took it as a parameter.
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 the commands together, try to find your own use case while building the Dockerfile and select the appropriate command.
Hurray! You made it till the end hope you got a better understanding of how
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.