Bird
Raised Fist0
Dockerdevops~5 mins

WORKDIR instruction for directory in Docker - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: WORKDIR instruction for directory
O(n)
Understanding Time 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?

Scenario Under Consideration

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.

Identify Repeating Operations

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

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.

Final Time Complexity

Time Complexity: O(n)

This means the time to process the WORKDIR instruction grows in a straight line as the directory path gets longer.

Common Mistake

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

Interview Connect

Understanding how simple instructions like WORKDIR scale helps you reason about build times and Dockerfile efficiency in real projects.

Self-Check

"What if we used multiple relative WORKDIR instructions instead of one long absolute path? How would the time complexity change?"

Practice

(1/5)
1. What does the WORKDIR instruction do in a Dockerfile?
easy
A. Runs a command when the container starts
B. Deletes files in the container
C. Sets the working directory for commands inside the container
D. Specifies the base image for the container

Solution

  1. Step 1: Understand the purpose of WORKDIR

    The WORKDIR instruction sets the folder where subsequent commands run inside the container.
  2. Step 2: Compare with other options

    Options A, B, and C describe other Dockerfile instructions or actions, not WORKDIR.
  3. Final Answer:

    Sets the working directory for commands inside the container -> Option C
  4. Quick Check:

    WORKDIR sets folder for commands [OK]
Hint: WORKDIR sets folder for commands inside container [OK]
Common Mistakes:
  • Confusing WORKDIR with CMD or ENTRYPOINT
  • Thinking WORKDIR deletes files
  • Assuming WORKDIR sets base image
2. Which of the following is the correct syntax to set the working directory to /app in a Dockerfile?
easy
A. WORKDIR /app
B. WORKDIR: /app
C. WORKDIR = /app
D. WORKDIR(/app)

Solution

  1. Step 1: Recall Dockerfile syntax for WORKDIR

    The correct syntax is WORKDIR /path without any symbols like colon, equals, or parentheses.
  2. Step 2: Check each option

    Options A, B, and D use invalid syntax for Dockerfile instructions.
  3. Final Answer:

    WORKDIR /app -> Option A
  4. Quick Check:

    WORKDIR uses space, no symbols [OK]
Hint: WORKDIR uses space before path, no symbols [OK]
Common Mistakes:
  • Adding colon or equals sign after WORKDIR
  • Using parentheses around path
  • Forgetting the space between WORKDIR and path
3. Given this Dockerfile snippet:
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?
medium
A. /app/data/file.txt
B. /app/message.txt
C. /message.txt
D. /app/data/message.txt

Solution

  1. Step 1: Follow WORKDIR changes

    The first WORKDIR sets directory to /app. The second WORKDIR changes it to /app/data.
  2. Step 2: Locate where message.txt is created

    The command RUN echo "hello" > message.txt runs in /app/data, so the file is created there.
  3. Final Answer:

    /app/data/message.txt -> Option D
  4. Quick Check:

    Last WORKDIR sets folder for commands [OK]
Hint: Last WORKDIR sets current folder for commands [OK]
Common Mistakes:
  • Assuming files created in first WORKDIR folder
  • Confusing file paths with root directory
  • Ignoring multiple WORKDIR instructions
4. You wrote this Dockerfile snippet:
WORKDIR /app
RUN ls
WORKDIR app/data
RUN ls

Why does the second WORKDIR cause an unexpected result?
medium
A. Because WORKDIR cannot be used twice
B. Because relative path 'app/data' resolves to /app/app/data instead of /app/data
C. Because RUN ls is invalid syntax
D. Because /app directory is not created automatically

Solution

  1. Step 1: Understand relative paths in WORKDIR

    The second WORKDIR uses a relative path app/data, which is relative to the current directory /app. So it tries to set /app/app/data.
  2. Step 2: Understand relative path resolution

    Docker creates /app/app/data automatically if needed, but this nested path is usually unintended if /app/data was desired.
  3. Step 3: Identify actual issue

    Relative path app/data is a mistake; use data or absolute /app/data instead.
  4. Final Answer:

    Because relative path 'app/data' resolves to /app/app/data instead of /app/data -> Option B
  5. Quick Check:

    Relative WORKDIR paths resolve from current dir [OK]
Hint: Relative WORKDIR paths are inside current folder [OK]
Common Mistakes:
  • Thinking WORKDIR cannot be used multiple times
  • Assuming RUN ls is invalid
  • Believing /app is not auto-created
5. You want to create a Dockerfile that:
- Sets working directory to /app
- Creates a subfolder logs
- Changes working directory to logs
- Creates a file app.log

Which Dockerfile snippet correctly does this?
hard
A. WORKDIR /app RUN mkdir logs WORKDIR logs RUN touch app.log
B. WORKDIR /app/logs RUN mkdir logs WORKDIR /app RUN touch app.log
C. RUN mkdir /app/logs WORKDIR /app/logs RUN touch app.log
D. WORKDIR /app RUN touch app.log WORKDIR logs RUN mkdir logs

Solution

  1. 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 /app and runs mkdir logs to create the subfolder.
  2. Step 2: Change WORKDIR to logs and create app.log

    Then it changes WORKDIR to logs (relative to /app) and creates app.log inside it.
  3. 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.
  4. Final Answer:

    WORKDIR /app RUN mkdir logs WORKDIR logs RUN touch app.log -> Option A
  5. Quick Check:

    Create folder before changing WORKDIR [OK]
Hint: Create folder before changing WORKDIR into it [OK]
Common Mistakes:
  • Changing WORKDIR before folder exists
  • Creating files in wrong directory
  • Using absolute path incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard