CMD instruction for default command in Docker - Time & Space Complexity
We want to understand how the time it takes to run a Docker container changes when using the CMD instruction.
Specifically, how does the default command affect execution time as input changes?
Analyze the time complexity of this Dockerfile snippet.
FROM ubuntu:latest
CMD ["echo", "Hello World"]
This sets a default command that prints "Hello World" when the container runs.
Look for any repeated actions inside the CMD instruction.
- Primary operation: Running the echo command once.
- How many times: Exactly one time per container start.
The command runs once regardless of input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The number of operations stays the same no matter the input size.
Time Complexity: O(1)
This means the command runs in constant time, no matter how big or small the input is.
[X] Wrong: "The CMD command runs multiple times or depends on input size."
[OK] Correct: The CMD instruction runs only once when the container starts, so its execution time does not grow with input size.
Understanding how default commands run helps you explain container startup behavior clearly and confidently.
What if the CMD runs a script that loops over a list of files? How would the time complexity change?
Practice
CMD instruction in a Dockerfile?Solution
Step 1: Understand the role of CMD
TheCMDinstruction specifies the default command to run inside the container when it starts.Step 2: Differentiate from other instructions
Other instructions likeCOPY,EXPOSE, andENVserve different purposes unrelated to the default command.Final Answer:
To set the default command that runs when the container starts -> Option BQuick Check:
CMD sets default command = C [OK]
- Confusing CMD with COPY or ENV
- Thinking CMD exposes ports
- Assuming CMD sets environment variables
CMD instruction to run nginx as the default command?Solution
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.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.Final Answer:
CMD ["nginx"] -> Option AQuick Check:
CMD JSON array uses brackets and quotes = D [OK]
- Using parentheses or curly braces instead of brackets
- Omitting quotes around command
- Using shell form without quotes causing errors
FROM alpine CMD ["echo", "Hello World"]
What will be the output when you run the container without overriding the command?
Solution
Step 1: Understand CMD execution
The CMD instruction runsechowith argumentHello Worldas the default command inside the container.Step 2: Predict container output
Running the container executesecho "Hello World", which printsHello Worldto the console.Final Answer:
Hello World -> Option CQuick Check:
CMD runs echo with args = Hello World [OK]
- Thinking CMD prints the command itself
- Assuming no output without explicit print
- Confusing CMD with ENTRYPOINT behavior
FROM ubuntu CMD echo Hello World
Solution
Step 1: Check CMD syntax
The CMD instruction here uses shell form without quotes, which is allowed but less safe and less clear.Step 2: Recommend JSON array form
Best practice is to use JSON array form likeCMD ["echo", "Hello World"]to avoid shell parsing issues.Final Answer:
CMD should be in JSON array form for best practice -> Option DQuick Check:
Use JSON array for CMD best practice = B [OK]
- Thinking shell form is invalid syntax
- Assuming quotes are mandatory in shell form
- Believing CMD cannot run simple commands
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?Solution
Step 1: Understand CMD override
When running a container, any command after the image name overrides the Dockerfile CMD instruction.Step 2: Identify correct docker run syntax
The correct syntax isdocker run myimage node test.js. Options with CMD or --cmd flags are invalid.Final Answer:
docker run myimage node test.js -> Option AQuick Check:
Override CMD by adding command after image name = A [OK]
- Using CMD or --cmd flags in docker run
- Placing command before image name
- Using invalid docker run syntax
Ready to prove it?
Pass the test and this pattern moves unknown
