RUN instruction for executing commands in Docker - Time & Space Complexity
We want to understand how the time it takes to run commands inside a Docker image grows as we add more commands.
How does adding more commands affect the total build time?
Analyze the time complexity of the following Dockerfile snippet using RUN instructions.
FROM ubuntu:latest
RUN apt-get update && apt-get install -y curl
RUN echo "Hello World"
RUN mkdir /app
RUN touch /app/file1 /app/file2 /app/file3
This snippet runs several commands one after another during the image build.
Look for repeated command executions or loops.
- Primary operation: Each RUN instruction executes commands sequentially.
- How many times: The number of RUN instructions equals the number of command groups run.
As you add more RUN instructions, the total build time grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 2 RUN commands | 2 command executions |
| 5 RUN commands | 5 command executions |
| 10 RUN commands | 10 command executions |
Pattern observation: Doubling the number of RUN instructions roughly doubles the total execution time.
Time Complexity: O(n)
This means the total time grows linearly with the number of RUN commands you add.
[X] Wrong: "Adding more RUN commands won't affect build time much because they run fast."
[OK] Correct: Each RUN command adds extra steps and time, so more commands mean longer builds.
Understanding how build steps add up helps you write efficient Dockerfiles and shows you think about performance in real projects.
"What if we combined multiple commands into a single RUN instruction? How would the time complexity change?"
Practice
RUN instruction do in a Dockerfile?Solution
Step 1: Understand Dockerfile instructions
TheRUNinstruction is used to execute commands while building the Docker image.Step 2: Differentiate from other instructions
Other instructions likeCOPYorENVserve different purposes, not command execution.Final Answer:
Executes commands during the image build process -> Option AQuick Check:
RUN = execute commands during build [OK]
- Confusing RUN with CMD or ENTRYPOINT
- Thinking RUN starts containers
- Mixing RUN with COPY or ENV
RUN in a Debian-based Docker image?Solution
Step 1: Understand package installation in Debian
Before installing packages, you must update the package list usingapt-get update.Step 2: Use correct flags and chaining
The-yflag auto-confirms installation, and chaining commands with&&ensures update runs before install.Final Answer:
RUN apt-get update && apt-get install -y curl -> Option BQuick Check:
Update then install with -y flag [OK]
- Skipping apt-get update
- Not using -y to auto-confirm
- Wrong command order or syntax
FROM ubuntu:latest RUN echo "Hello" && echo "World"
What will be the output when building this Docker image?
Solution
Step 1: Analyze the RUN command chaining
The command uses&&to run two echo commands sequentially during build.Step 2: Understand Docker build output
During build, the output of RUN commands is shown in the terminal, so both 'Hello' and 'World' will print.Final Answer:
Hello World printed during build -> Option AQuick Check:
RUN shows command output during build [OK]
- Thinking RUN output is hidden
- Confusing && with ;
- Assuming only first command runs
RUN apt-get update & apt-get install -y git
Solution
Step 1: Check command chaining syntax
Single&runs command in background and is invalid in Docker RUN;&&is needed to chain commands properly.Step 2: Confirm correct chaining for sequential execution
Using&&ensures the second command runs only if the first succeeds.Final Answer:
Using single & instead of && causes syntax error -> Option DQuick Check:
Use && to chain commands in RUN [OK]
- Using & instead of &&
- Ignoring command chaining rules
- Assuming RUN runs commands in parallel
Solution
Step 1: Understand Docker image layers
Each RUN instruction creates a new image layer, increasing image size.Step 2: Combine commands to reduce layers
Chaining commands with&&inside one RUN instruction runs them sequentially in a single layer.Final Answer:
Chain commands with && inside one RUN instruction -> Option CQuick Check:
Combine commands with && to reduce layers [OK]
- Using multiple RUNs increases layers
- Using CMD instead of RUN for build steps
- Separating commands with semicolons without chaining
Ready to prove it?
Pass the test and this pattern moves unknown
