WORKDIR instruction for directory in Docker - Time & Space Complexity
We want to understand how the time to set a working directory in Docker changes as the directory path length or complexity grows.
How does Docker handle the WORKDIR instruction when given different directory paths?
Analyze the time complexity of the following Dockerfile snippet.
FROM alpine:latest
WORKDIR /app
WORKDIR logs
WORKDIR /var/www/html
This snippet sets the working directory multiple times, using both absolute and relative paths.
Look at how Docker processes each WORKDIR instruction.
- Primary operation: Resolving the directory path for each WORKDIR instruction.
- How many times: Once per WORKDIR instruction in the Dockerfile.
As the directory path length increases, Docker must process more parts of the path.
| Input Size (path depth) | Approx. Operations |
|---|---|
| 1 (e.g., /app) | 1 operation |
| 3 (e.g., /var/www/html) | 3 operations |
| 10 (e.g., /a/b/c/d/e/f/g/h/i/j) | 10 operations |
Pattern observation: The time to resolve the directory grows linearly with the number of directory levels.
Time Complexity: O(n)
This means the time to process the WORKDIR instruction grows in a straight line as the directory path gets longer.
[X] Wrong: "Setting WORKDIR always takes the same time no matter how long the path is."
[OK] Correct: Docker must process each directory level in the path, so longer paths take more steps.
Understanding how simple instructions like WORKDIR scale helps you reason about build times and Dockerfile efficiency in real projects.
"What if we used multiple relative WORKDIR instructions instead of one long absolute path? How would the time complexity change?"
Practice
WORKDIR instruction do in a Dockerfile?Solution
Step 1: Understand the purpose of WORKDIR
The WORKDIR instruction sets the folder where subsequent commands run inside the container.Step 2: Compare with other options
Options A, B, and C describe other Dockerfile instructions or actions, not WORKDIR.Final Answer:
Sets the working directory for commands inside the container -> Option CQuick Check:
WORKDIR sets folder for commands [OK]
- Confusing WORKDIR with CMD or ENTRYPOINT
- Thinking WORKDIR deletes files
- Assuming WORKDIR sets base image
/app in a Dockerfile?Solution
Step 1: Recall Dockerfile syntax for WORKDIR
The correct syntax isWORKDIR /pathwithout any symbols like colon, equals, or parentheses.Step 2: Check each option
Options A, B, and D use invalid syntax for Dockerfile instructions.Final Answer:
WORKDIR /app -> Option AQuick Check:
WORKDIR uses space, no symbols [OK]
- Adding colon or equals sign after WORKDIR
- Using parentheses around path
- Forgetting the space between WORKDIR and path
FROM alpine WORKDIR /app RUN touch file.txt WORKDIR /app/data RUN echo "hello" > message.txt
Where will the file
message.txt be created inside the container?Solution
Step 1: Follow WORKDIR changes
The first WORKDIR sets directory to/app. The second WORKDIR changes it to/app/data.Step 2: Locate where message.txt is created
The commandRUN echo "hello" > message.txtruns in/app/data, so the file is created there.Final Answer:
/app/data/message.txt -> Option DQuick Check:
Last WORKDIR sets folder for commands [OK]
- Assuming files created in first WORKDIR folder
- Confusing file paths with root directory
- Ignoring multiple WORKDIR instructions
WORKDIR /app RUN ls WORKDIR app/data RUN ls
Why does the second
WORKDIR cause an unexpected result?Solution
Step 1: Understand relative paths in WORKDIR
The second WORKDIR uses a relative pathapp/data, which is relative to the current directory/app. So it tries to set/app/app/data.Step 2: Understand relative path resolution
Docker creates/app/app/dataautomatically if needed, but this nested path is usually unintended if /app/data was desired.Step 3: Identify actual issue
Relative pathapp/datais a mistake; usedataor absolute/app/datainstead.Final Answer:
Because relative path 'app/data' resolves to /app/app/data instead of /app/data -> Option BQuick Check:
Relative WORKDIR paths resolve from current dir [OK]
- Thinking WORKDIR cannot be used multiple times
- Assuming RUN ls is invalid
- Believing /app is not auto-created
- Sets working directory to
/app- Creates a subfolder
logs- Changes working directory to
logs- Creates a file
app.logWhich Dockerfile snippet correctly does this?
Solution
Step 1: Set working directory to /app and create logs folder
WORKDIR /app RUN mkdir logs WORKDIR logs RUN touch app.log sets WORKDIR to/appand runsmkdir logsto create the subfolder.Step 2: Change WORKDIR to logs and create app.log
Then it changes WORKDIR tologs(relative to /app) and createsapp.loginside it.Step 3: Verify other options
WORKDIR /app/logs RUN mkdir logs WORKDIR /app RUN touch app.log sets WORKDIR incorrectly and creates logs folder in wrong place. RUN mkdir /app/logs WORKDIR /app/logs RUN touch app.log creates logs folder but misses initial WORKDIR. WORKDIR /app RUN touch app.log WORKDIR logs RUN mkdir logs creates app.log before logs folder and changes WORKDIR before folder exists.Final Answer:
WORKDIR /app RUN mkdir logs WORKDIR logs RUN touch app.log -> Option AQuick Check:
Create folder before changing WORKDIR [OK]
- Changing WORKDIR before folder exists
- Creating files in wrong directory
- Using absolute path incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
