Bird
Raised Fist0
Dockerdevops~20 mins

CMD instruction for default command in Docker - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
CMD Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Dockerfile CMD instruction?
Given the Dockerfile snippet below, what will be the output when running the container without any additional command?
Docker
FROM alpine
CMD ["echo", "Hello World"]
Aecho Hello World
BNo output, container exits immediately
CHello World
DError: CMD syntax invalid
Attempts:
2 left
💡 Hint
CMD defines the default command and arguments for the container.
🧠 Conceptual
intermediate
2:00remaining
What happens if you specify CMD multiple times in a Dockerfile?
Consider a Dockerfile with multiple CMD instructions. Which statement is true about how Docker handles them?
AOnly the last CMD instruction is used as the default command.
BAll CMD instructions run sequentially when the container starts.
CDocker throws an error during build for multiple CMD instructions.
DThe first CMD instruction is used and others are ignored.
Attempts:
2 left
💡 Hint
Think about how Docker layers and instructions override each other.
🔀 Workflow
advanced
2:00remaining
How to override the CMD default command when running a container?
You have a Docker image with CMD ["python", "app.py"]. How can you run the container to execute 'python test.py' instead?
Adocker run image_name -c python test.py
Bdocker run image_name python test.py
Cdocker run image_name CMD python test.py
Ddocker run image_name --cmd python test.py
Attempts:
2 left
💡 Hint
The CMD can be overridden by specifying a command after the image name.
❓ Troubleshoot
advanced
2:00remaining
Why does this Docker container exit immediately after starting?
Dockerfile: FROM ubuntu CMD ["python"] You run the container but it exits immediately. What is the most likely cause?
AThe python command is not installed in the image.
BThe container has no foreground process and exits immediately.
CThe CMD syntax is incorrect and causes an error.
DDocker daemon is not running.
Attempts:
2 left
💡 Hint
Check if the command in CMD exists inside the container.
✅ Best Practice
expert
2:00remaining
What is the recommended form of CMD instruction for best compatibility?
Which CMD instruction form is best practice to avoid shell processing issues and signal handling problems?
ACMD echo "Hello World"
BCMD echo Hello World
CCMD /bin/sh -c "echo Hello World"
DCMD ["echo", "Hello World"]
Attempts:
2 left
💡 Hint
Consider how Docker executes commands and handles signals.

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