There are certain Docker instructions that can cause confusion. One such case is the Docker CMD vs Entrypoint instructions.

In this post, we will look at the difference between Docker CMD and Entrypoint instructions. Also, we will look at when to use what.

If you are completely new to Docker, you can also start from our detailed post on Docker Step-by-Step Learning.

Docker Image Build Process

Basically, when we run a Docker container, Docker actually runs an image inside that container.

Docker images are built using step-by-step instructions. We mention these instructions in a Dockerfile. You can refer to Create a Dockerfile from scratch for a NodeJS application to understand how to create a Dockerfile.

Each step in the Dockerfile adds a layer to the existing image. In other words, we start from a base image (specifying probably the host OS) and add layers on top of it in each step. Basically, you can think of the Docker image as an onion.

CMD and Entrypoint instructions are two of the most important instructions you can mention in a Dockerfile. Basically, these instructions tell Docker what to do when running a container with a Docker image.

The CMD Instruction

The CMD instruction sets the default command or parameters for a Docker container. However, we can override the command or its parameters from the command line when we run the Docker container. In other words, the default command executes only if we run the Docker image without a command.

Also, if the Dockerfile has multiple commands, only the last one is considered. Docker ignores the other commands.

CMD instruction has 2 forms:

  • The Shell Form – In this form, we can write the CMD command as below:
CMD command param1 param2

As a more concrete example, see below:

CMD echo "hello world"

When we run the image containing the above command with the Docker run -it <image>, we will see the below response

hello world
  • The Exec Form

In exec form, we simply specify the Docker CMD instruction as below:

CMD ["executable", "param1"]

In another example:

CMD ["echo", "hello world"]

The Entrypoint Instruction

The Entrypoint instruction allows us to configure our container so that they run in executable mode. Basically, what this means is that we specify a command using ENTRYPOINT instruction and such a command cannot be overriden while running the container.

ENTRYPOINT also has 2 modes similar to the CMD instructions i.e. shell and exec.

An example from the exec form looks like below:

ENTRYPOINT ["executable", "param"]

However, shell form is as below:

ENTRYPOINT command param1

Using ENTRYPOINT and CMD Together

ENTRYPOINT and CMD instructions can also be used together.

However, in that case, we can’t override the command itself. Only the parameters for the CMD instruction can be overridden while executing Docker run statement.

Note than in this case, ENTRYPOINT can only be used in exec form. The shell mode of ENTRYPOINT will ignore any other CMD instructions.

Conclusion

To conclude our Docker CMD vs Entrypoint discussion, it is advisable to use ENTRYPOINT rather than CMD when building executable Docker images. This is because in such a case, the default command cannot be overridden.

Choosing CMD is useful only in cases where you wish to allow the default command to be overridden while running the container.

Categories: BlogDocker

Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *