What is the difference between CMD and ENTRYPOINT in a Dockerfile?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In the realm of Docker containers, understanding how to define what happens when a container starts is crucial. Two primary instructions found in a Dockerfile for this purpose are CMD and ENTRYPOINT. Although these directives are often misunderstood or used interchangeably, they serve different purposes. This article delineates the differences between CMD and ENTRYPOINT, covers their technical aspects, and explores their use through examples.
The CMD Instruction
The CMD instruction in a Dockerfile is utilized to specify the default command that should run when a container is started. It acts as a fallback command if no command is provided during the docker run execution. The CMD directive provides a way to include default parameters when starting a container but can be overridden by providing alternative arguments at runtime.
Examples of CMD Usage
In its basic form, the CMD instruction can be represented in three ways:
- Exec Form: Most recommended, as it prevents the underlying shell process and related issues.
- Shell Form: Executes the command using
/bin/sh -c.
- Inherited Form: If
CMDis defined as a parameter.
In a scenario where you use CMD like so:
This will instruct the container to run nginx with -g daemon off; as arguments unless specified otherwise in docker run.
The ENTRYPOINT Instruction
Contrariwise, ENTRYPOINT is used to define a command that will always be executed, opt-in to act as the immutable part of the command. The benefit of using ENTRYPOINT is ensuring that required binaries are executed when a container starts and maintaining more control over command execution when shipping containerized applications.
Examples of ENTRYPOINT Usage
Similar to CMD, ENTRYPOINT can also be used in:
- Exec Form: Preferred due to its robust nature, preventing shell interpretation.
- Shell Form: Converts the command to a shell command with
/bin/sh -c.
A Dockerfile example using ENTRYPOINT might look like:
Here, nginx will always run upon starting a container, with default parameters provided by CMD.
Combining ENTRYPOINT and CMD
You can combine ENTRYPOINT and CMD to create flexible entry points. ENTRYPOINT prepares the stage for command execution, while CMD acts as the default parameter provider. Consider this example:
When running docker run <image>, it echoes Hello, World!, but upon specifying arguments during runtime, like docker run <image> Hey Docker!, Hey Docker! overrides the CMD part, resulting in echo Hey Docker!.
Key Differences
| Aspect | CMD | ENTRYPOINT |
| Purpose | Default command/arguments for a container Can be overridden | Forces a command to run, controlling responsibility less flexible |
| Flexibility | Easily overridden by runtime command arguments | Less flexible, always executes even with runtime arguments |
| Use Case | Default behavior alterations e.g., script arguments | Fixed setup scripts, crucial being executed every time |
| Precaution | If used together, can lead to command execution failure | Must be paired with CMD
or explicit arguments in docker run |
When to Use Each
- Use
CMDwhen you need default behavior that can be changed at runtime. Ideal for default arguments, scripts, or application commands that may vary. - Use
ENTRYPOINTwhen you want a mandatory command always to run, regardless of user input. Vital for fixed setups or applications that must always start in a specific way.
Understanding the roles of CMD and ENTRYPOINT helps build more predictable and structured Docker containers. By combining these directives properly, you can leverage Docker's dynamic and versatile nature for effective containerization.
Related reading
- What is the difference between commands and container_commands configuration keys in Beanstalk?
- What is the difference between “container_memory_working_set_bytes” and “container_memory_rss” metric on the container
- What is the difference between docker-compose up and docker-compose start?
- What is the difference between docker and docker-compose
- What is the difference between Elastic Beanstalk and CloudFormation for a .NET project?
- What is the difference between gradle bootRun and gradle run to run a springboot application?
- What is the difference between Docker Swarm and Kubernetes/Mesophere?
- What is the difference between expose and publish in Docker?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.