0
0
Dockerdevops~5 mins

CMD instruction for default command in Docker - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: CMD instruction for default command
O(1)
Understanding Time 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?

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

The command runs once regardless of input size.

Input Size (n)Approx. Operations
101
1001
10001

Pattern observation: The number of operations stays the same no matter the input size.

Final Time Complexity

Time Complexity: O(1)

This means the command runs in constant time, no matter how big or small the input is.

Common Mistake

[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.

Interview Connect

Understanding how default commands run helps you explain container startup behavior clearly and confidently.

Self-Check

What if the CMD runs a script that loops over a list of files? How would the time complexity change?