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?