CMD vs ENTRYPOINT in Docker 🐳 | Key Differences Explained
This comprehensive study note explores two of the most important instructions in a Dockerfile: CMD and ENTRYPOINT. Mastering these instructions is crucial not only for building robust, configurable containerized applications but also for navigating container orchestration and technical interviews.
1. Why Do We Need Container Instructions?
When building container images, we need a way to define what command execution runs when a container starts from that image. While a Dockerfile sets up the filesystem, dependencies, and environment, instructions like CMD and ENTRYPOINT dictate the runtime executable behavior.
Understanding how these instructions handle arguments, defaults, and runtime overrides prevents unexpected application startup failures in production environments.
2. Core Mental Model & Behavior Summary
The fundamental distinction between CMD and ENTRYPOINT lies in how they handle command-line arguments passed during a docker run execution:
CMD: Acts as a default command that can be completely overwritten at runtime when you pass alternative arguments todocker run.ENTRYPOINT: Acts as a fixed executable where any arguments passed duringdocker runare appended to the entrypoint command rather than replacing it.- Combined (
ENTRYPOINT+CMD): When used together,ENTRYPOINTserves as the base executable command, andCMDsupplies the default arguments to that executable.
3. Deep Dive into CMD (Default Overridable Command)
The CMD instruction provides defaults for an executing container. If you specify CMD in your Dockerfile, those defaults can be seamlessly overridden by providing a new command string when running the container.
Step-by-Step Hands-On: CMD Demo
-
Create the
Dockerfile:DOCKERFILEFROM ubuntu CMD ["echo", "hello from CMD"] -
Build the Docker Image:
BASHdocker build -t cmd-demo . -
Run the Container (Using Default
CMD):BASHdocker run cmd-demo- Output:
TEXT
hello from CMD
- Output:
-
Override the Default Command at Runtime:
BASHdocker run cmd-demo echo "hi another"- Output:
TEXT
hi another
Note: Notice how the entire default command (
hello from CMD) was completely replaced by the runtime argument (hi another). - Output:
4. Deep Dive into ENTRYPOINT (Fixed Executable with Argument Appending)
Unlike CMD, the ENTRYPOINT instruction configures a container that will run as an executable. When you pass arguments to docker run with an entrypoint image, those arguments are appended to the entrypoint command.
Working with Custom Dockerfile Names (-f Flag)
When testing multiple Dockerfiles in the same directory, you can name them uniquely (e.g., new-Dockerfile) and reference them during the build process using the -f flag.
Step-by-Step Hands-On: ENTRYPOINT Demo
-
Create a Custom
new-Dockerfile:DOCKERFILEFROM ubuntu ENTRYPOINT ["echo", "hello from entrypoint"] -
Build the Image Using
-f:BASHdocker build -t entrypoint-demo -f new-Dockerfile . -
Run the Container (Default Behavior):
BASHdocker run entrypoint-demo- Output:
TEXT
hello from entrypoint
- Output:
-
Attempt to Override/Append at Runtime:
BASHdocker run entrypoint-demo "try override"- Output:
TEXT
hello from entrypoint try override
Note: Instead of replacing the original text (like
CMDdid),ENTRYPOINTkept the original command and appended"try override"to the end. - Output:
5. Combining ENTRYPOINT and CMD
You can combine both instructions in a single Dockerfile. In this architectural pattern, ENTRYPOINT defines the base binary or command, and CMD provides the default arguments that can be overridden at runtime.
Step-by-Step Hands-On: Combo Demo
-
Create a
comboDockerfile:DOCKERFILEFROM ubuntu ENTRYPOINT ["echo"] CMD ["hello from combo file"] -
Build the Combo Image:
BASHdocker build -t combo-demo -f combo . -
Run without Runtime Arguments (Uses Default
CMD):BASHdocker run combo-demo- Output:
TEXT
hello from combo file
- Output:
-
Run with Runtime Arguments (Overrides
CMDarguments):BASHdocker run combo-demo "hi Shubham"- Output:
TEXT
hi Shubham
Note: Here,
echoremained the fixedENTRYPOINT, while"hello from combo file"was replaced by"hi Shubham"supplied via the runtime override. - Output:
6. Comparative Synthesis: CMD vs ENTRYPOINT
7. Summary and Key Takeaways
CMDDefault Behavior: Serves as a default command that is completely overwritten when new arguments are passed todocker run.ENTRYPOINTBehavior: Configures a container to run as an executable where runtime arguments are appended rather than replacing the instruction.- Custom Dockerfile Names: You can build images from non-standard filenames using the Docker build flag
-f <filename>. - Overriding
CMD: Passingdocker run image-name new-commandcompletely discards theCMDstring defined in the image. - Appending
ENTRYPOINT: Passing arguments to an entrypoint-enabled container attaches those arguments directly to the end of the entrypoint array. - Synergistic Combo Pattern: Using
ENTRYPOINTfor the core executable andCMDfor default arguments gives container users maximum flexibility without sacrificing base execution logic. - Interview Relevance: Clearly understanding how runtime arguments interact with
CMDvsENTRYPOINTis a standard fundamental question in DevOps and containerization interviews.