Understanding CMD vs ENTRYPOINT Commands in Docker File

Rhegisan Jebas
4 min readApr 4, 2023

--

INTRODUCTION

Docker is a popular containerization platform that allows developers to package their applications and run them in a consistent and portable environment. One of the key features of Docker is the ability to use CMD and ENTRYPOINT commands to define how the container should be run.

In this blog post, we’ll explore the difference between CMD and ENTRYPOINT commands in Docker, using the echo command as an example. We’ll also explain how to use these commands together to create more flexible Docker images.

CMD Command

The CMD command is used to provide default arguments to the command specified in the ENTRYPOINT command. The default argument can be overridden at runtime by providing additional arguments to the docker run command.

Let’s take a look at an example Docker file using the echo command:

FROM ubuntu:latest
CMD ["echo", "Hey Rhegi"]

In this example, we’re using the ubuntu:latest image as our base image. We’re then using the CMD command to provide a default command of echo Hey Rhegi. This means that when a container is started from this image, the message “Hey Rhegi” will be printed to the console.

To build and run this Docker image, use the following commands:

$ docker build -t cmdimage .
$ docker run cmdimage

This will start a container from the cmdimage image and print the message “Hey Rhegi” to the console.

If you want to provide a different argument to the echo command, you can do so by passing it as an argument to the docker run command. For example:

$ docker run cmdimage "Hey Shawn"

This will start a container from the cmdimage image and print the message “Hey Shawn” to the console.

ENTRYPOINT COMMAND

The ENTRYPOINT command is used to specify the command that should be run when the container starts. This command is the main command of the container and cannot be overridden at runtime.

Let’s take a look at an example Dockerfile using the echo command:

FROM ubuntu:latest
ENTRYPOINT ["echo", "Hey"]

In this example, we’re using the ubuntu:latest image as our base image. We’re then using the ENTRYPOINT command to set the default command to echo Hey. This means that when a container is started from this image, the message “Hey” will be printed to the console.

To build and run this Docker image, use the following commands:

$ docker build -t entrypointimage .
$ docker run entrypointimage

This will start a container from the entrypointimage image and print the message “Hey” to the console.

If you want to provide a different argument to the echo command, you cannot do so by passing it as an argument to the docker run command because the ENTRYPOINT command specifies the main command of the container. However, you can provide additional arguments to the main command using the CMD command.

USING CMD AND ENTRYPOINT TOGETHER

Now, let’s say we want to use both the CMD and ENTRYPOINT commands together to provide a default command with default arguments, while still allowing users to specify additional arguments.

Here’s an example Dockerfile using both the CMD and ENTRYPOINT commands:

FROM ubuntu:latest
ENTRYPOINT ["echo", "Hey"]
CMD ["Rhegi"]

In this example, we’re using the same Dockerfile as before. The ENTRYPOINT command is still set to echo Hey, and the CMD command is still set to Rhegi.

To build and run this Docker image, use the following commands:

$ docker build -t combinedimage .
$ docker run combinedimage

This will start a container from the combinedimage image and print the message “Hey Rhegi” to the console.

If you want to provide a different argument to the echo command, you can do so by passing it as an argument to the docker run command. For example:

$ docker run combinedimage "Shawn"

This will start a container from the combinedimage image and print the message “Hey Shawn” to the console.

Notice how the default argument provided by the CMD command is overridden by the argument provided at runtime.

Conclusion

In conclusion, CMD and ENTRYPOINT commands are important tools for defining how your Docker container should be run. While the CMD command provides default arguments that can be overridden at runtime, the ENTRYPOINT command sets the main command of the container and cannot be overridden.

By using both commands together, you can create more flexible Docker images that provide default arguments while still allowing users to specify additional arguments at runtime.

Hopefully, this blog post has helped you understand the difference between CMD and ENTRYPOINT commands in Docker, and how to use them effectively in your Dockerfiles.

If you have any questions or comments, please don’t hesitate to leave them in the comments section below. I’ll do my best to respond to each one.

If you’d like to stay up to date on my latest automation-related content, be sure to follow me on LinkedIn. I’d love to connect with you and hear your thoughts on automation in the workplace.

--

--

Rhegisan Jebas

Passionate DevOps engineer sharing insights on building high-quality software through automation, containerization, and continuous delivery.