Bird
Raised Fist0
Dockerdevops~10 mins

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

Choose your learning style10 modes available
Process Flow - CMD instruction for default command
Dockerfile with CMD
↓
Build Docker Image
↓
Run Container
↓
Check if CMD is overridden?
Yes→Run 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.

Practice

(1/5)
1. What is the main purpose of the CMD instruction in a Dockerfile?
easy
A. To expose network ports
B. To set the default command that runs when the container starts
C. To copy files into the container
D. To define environment variables

Solution

  1. Step 1: Understand the role of CMD

    The CMD instruction specifies the default command to run inside the container when it starts.
  2. Step 2: Differentiate from other instructions

    Other instructions like COPY, EXPOSE, and ENV serve different purposes unrelated to the default command.
  3. Final Answer:

    To set the default command that runs when the container starts -> Option B
  4. Quick Check:

    CMD sets default command = C [OK]
Hint: CMD sets container's default run command [OK]
Common Mistakes:
  • Confusing CMD with COPY or ENV
  • Thinking CMD exposes ports
  • Assuming CMD sets environment variables
2. Which of the following is the correct exec form syntax for the CMD instruction to run nginx as the default command?
easy
A. CMD ["nginx"]
B. CMD {"nginx"}
C. CMD ("nginx")
D. CMD nginx

Solution

  1. Step 1: Recall CMD syntax forms

    Docker supports two CMD forms: shell form (string) and exec form (JSON array). The exec form is recommended for clarity and safety.
  2. Step 2: Identify correct JSON array syntax

    The correct JSON array syntax uses square brackets and double quotes: CMD ["nginx"]. Other options use invalid syntax.
  3. Final Answer:

    CMD ["nginx"] -> Option A
  4. Quick Check:

    CMD JSON array uses brackets and quotes = D [OK]
Hint: Use JSON array with brackets and quotes for CMD [OK]
Common Mistakes:
  • Using parentheses or curly braces instead of brackets
  • Omitting quotes around command
  • Using shell form without quotes causing errors
3. Given this Dockerfile snippet:
FROM alpine
CMD ["echo", "Hello World"]

What will be the output when you run the container without overriding the command?
medium
A. No output, container exits silently
B. echo Hello World
C. Hello World
D. Error: command not found

Solution

  1. Step 1: Understand CMD execution

    The CMD instruction runs echo with argument Hello World as the default command inside the container.
  2. Step 2: Predict container output

    Running the container executes echo "Hello World", which prints Hello World to the console.
  3. Final Answer:

    Hello World -> Option C
  4. Quick Check:

    CMD runs echo with args = Hello World [OK]
Hint: CMD JSON array runs command with args exactly [OK]
Common Mistakes:
  • Thinking CMD prints the command itself
  • Assuming no output without explicit print
  • Confusing CMD with ENTRYPOINT behavior
4. Identify the best practice issue in this Dockerfile snippet:
FROM ubuntu
CMD echo Hello World
medium
A. Missing quotes around Hello World
B. FROM instruction is incorrect
C. CMD cannot run echo command
D. CMD should be in JSON array form for best practice

Solution

  1. Step 1: Check CMD syntax

    The CMD instruction here uses shell form without quotes, which is allowed but less safe and less clear.
  2. Step 2: Recommend JSON array form

    Best practice is to use JSON array form like CMD ["echo", "Hello World"] to avoid shell parsing issues.
  3. Final Answer:

    CMD should be in JSON array form for best practice -> Option D
  4. Quick Check:

    Use JSON array for CMD best practice = B [OK]
Hint: Prefer JSON array form for CMD to avoid shell issues [OK]
Common Mistakes:
  • Thinking shell form is invalid syntax
  • Assuming quotes are mandatory in shell form
  • Believing CMD cannot run simple commands
5. You have this Dockerfile:
FROM node:18
CMD ["node", "app.js"]

You want to override the default command to run node test.js when starting the container. Which command will do this correctly?
hard
A. docker run myimage node test.js
B. docker run myimage CMD ["node", "test.js"]
C. docker run myimage --cmd node test.js
D. docker run myimage run node test.js

Solution

  1. Step 1: Understand CMD override

    When running a container, any command after the image name overrides the Dockerfile CMD instruction.
  2. Step 2: Identify correct docker run syntax

    The correct syntax is docker run myimage node test.js. Options with CMD or --cmd flags are invalid.
  3. Final Answer:

    docker run myimage node test.js -> Option A
  4. Quick Check:

    Override CMD by adding command after image name = A [OK]
Hint: Override CMD by adding command after image name in docker run [OK]
Common Mistakes:
  • Using CMD or --cmd flags in docker run
  • Placing command before image name
  • Using invalid docker run syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard