0
0
Dockerdevops~10 mins

CMD instruction for default command in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - CMD instruction for default command
Dockerfile with CMD
Build Docker Image
Run Container
Check if CMD is overridden?
YesRun overridden command
No
Run CMD default command
Container runs with CMD output
This flow shows how Docker uses the CMD instruction as the default command when running a container unless overridden.
Execution Sample
Docker
FROM alpine
CMD ["echo", "Hello from CMD"]
This Dockerfile sets a default command to print a message when the container runs.
Process Table
StepActionCommand UsedOutputNotes
1Build image from DockerfileN/AImage createdImage includes CMD instruction
2Run container without command overrideecho Hello from CMDHello from CMDCMD runs as default
3Run container with command overrideecho Overridden commandOverridden commandCMD ignored, override used
4Container exits after command completesN/AContainer stoppedNormal container lifecycle ends
💡 Container stops after running the command (default CMD or override).
Status Tracker
VariableStartAfter BuildAfter Run (no override)After Run (override)Final
Command to runNoneecho Hello from CMDecho Hello from CMDecho Overridden commandN/A
Key Moments - 3 Insights
Why does the container run the CMD command when no command is given at runtime?
Because the CMD instruction in the Dockerfile sets the default command, as shown in execution_table step 2.
What happens if I provide a command when running the container?
The provided command overrides CMD, so the container runs the new command instead, as seen in execution_table step 3.
Does CMD run during image build?
No, CMD only runs when the container starts, not during image build (execution_table step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what command runs when the container is started without override?
ANo command runs
Becho Hello from CMD
Cecho Overridden command
DContainer build command
💡 Hint
Check execution_table row 2 under 'Command Used'
At which step does the container run a command different from the CMD instruction?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table rows where 'Command Used' differs from CMD
If you remove the CMD instruction from the Dockerfile, what happens when you run the container without a command?
AContainer fails to start
BContainer runs the last command used
CContainer runs with no output
DContainer runs default shell
💡 Hint
CMD sets default command; without it, no command runs unless overridden
Concept Snapshot
Docker CMD sets the default command for a container.
It runs when no command is given at container start.
CMD can be overridden by providing a command at runtime.
CMD does not run during image build.
Syntax: CMD ["executable", "param1", "param2"]
Full Transcript
The CMD instruction in a Dockerfile defines the default command that runs when a container starts. When you build an image with CMD and run a container without specifying a command, Docker runs the CMD command. If you provide a command when running the container, it overrides CMD. CMD does not execute during image build, only at container start. This behavior helps set default behavior while allowing flexibility.