Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between CMD and ENTRYPOINT in a Dockerfile?
Docker containers are built using Dockerfiles which contain step-by-step instructions to define the container environment. Among the various Dockerfile instructions, CMD and ENTRYPOINT are two critical commands that define what processes run inside containers. While they appear similar, they have distinct behaviors and use cases.
Before exploring their differences, it's important to understand the two forms of writing instructions in Dockerfiles:
Shell Form vs Executable Form
Shell Form
In shell form, the command is processed by a shell (/bin/sh -c). This allows shell features like variable expansion and command substitution.
INSTRUCTION command param1 param2
Executable Form
In executable form, the command runs directly without shell processing. This is the preferred form for CMD and ENTRYPOINT as it avoids signal handling issues.
INSTRUCTION ["executable", "param1", "param2"]
CMD Instruction
The CMD instruction sets default commands and parameters for the container. It executes only when no command is specified during docker run.
Key Characteristics:
Provides default command for container startup
Can be overridden by CLI arguments in
docker runOnly the last CMD instruction in a Dockerfile takes effect
CMD has three forms:
# Executable form (preferred) CMD ["executable", "param1", "param2"] # Shell form CMD command param1 param2 # As default parameters for ENTRYPOINT CMD ["param1", "param2"]
Example
FROM ubuntu:20.04 CMD ["echo", "Hello from CMD"]
$ docker run myimage Hello from CMD $ docker run myimage echo "Override CMD" Override CMD
ENTRYPOINT Instruction
The ENTRYPOINT instruction sets the main command that will always execute when the container starts. Unlike CMD, it cannot be overridden by CLI arguments.
Key Characteristics:
Always executes, cannot be overridden by CLI arguments
CLI arguments are appended as parameters
Can work with CMD to provide default parameters
ENTRYPOINT has two forms:
# Executable form (preferred) ENTRYPOINT ["executable", "param1", "param2"] # Shell form ENTRYPOINT command param1 param2
Example
FROM ubuntu:20.04 ENTRYPOINT ["echo", "Hello from ENTRYPOINT"]
$ docker run myimage Hello from ENTRYPOINT $ docker run myimage "additional text" Hello from ENTRYPOINT additional text
Comparison
| Feature | CMD | ENTRYPOINT |
|---|---|---|
| Override Behavior | Can be overridden by CLI args | Cannot be overridden |
| CLI Arguments | Replace the CMD entirely | Appended as parameters |
| Use Case | Default commands | Main application command |
| Flexibility | High (easy to override) | Low (consistent execution) |
CMD + ENTRYPOINT Combined
When both are used together, ENTRYPOINT defines the main command and CMD provides default parameters:
FROM ubuntu:20.04 ENTRYPOINT ["echo"] CMD ["Hello World"]
$ docker run myimage Hello World $ docker run myimage "Custom Message" Custom Message
Common Use Cases
Use CMD when:
You want flexible container behavior
Different commands may be needed at runtime
Building general-purpose base images
Use ENTRYPOINT when:
Container runs a specific application
Consistent behavior is required
CLI arguments should be treated as parameters
Conclusion
CMD provides default commands that can be overridden, making containers flexible. ENTRYPOINT ensures consistent execution of the main application, treating CLI arguments as parameters. Choose CMD for flexibility and ENTRYPOINT for consistency, or combine both for optimal control over container behavior.
