Bird
Raised Fist0
Dockerdevops~5 mins

WORKDIR instruction for directory in Docker - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the WORKDIR instruction do in a Dockerfile?
WORKDIR sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD instructions that follow it in the Dockerfile. It changes the current directory inside the container.
Click to reveal answer
beginner
If the directory specified in WORKDIR does not exist, what happens?
Docker will create the directory automatically before setting it as the working directory.
Click to reveal answer
intermediate
How does WORKDIR affect the path for subsequent instructions in a Dockerfile?
After setting WORKDIR, all relative paths in following instructions are based on this directory, making file operations easier and clearer.
Click to reveal answer
intermediate
Can you use multiple WORKDIR instructions in a Dockerfile? What happens?
Yes, you can use multiple WORKDIR instructions. Each one changes the working directory from the previous one. If a relative path is given, it is relative to the current WORKDIR.
Click to reveal answer
intermediate
Example: What is the working directory after these instructions? WORKDIR /app WORKDIR logs
The final working directory is /app/logs because the second WORKDIR 'logs' is relative to the first '/app'.
Click to reveal answer
What does the WORKDIR instruction do in a Dockerfile?
AExposes a port for the container
BCopies files into the container
CSets the working directory for following instructions
DRuns a command inside the container
If the directory in WORKDIR does not exist, what does Docker do?
AThrows an error and stops building
BUses the root directory instead
CIgnores the instruction
DCreates the directory automatically
What is the working directory after these instructions? WORKDIR /usr WORKDIR local
A/usr/local
B/local
C/usr
D/
Can you use multiple WORKDIR instructions in one Dockerfile?
ANo, only one WORKDIR is allowed
BYes, each changes the working directory
CYes, but only the first one works
DNo, it causes a build error
Which instruction uses the current WORKDIR as base for relative paths?
AAll of the above
BRUN
CWORKDIR
DCOPY
Explain what the WORKDIR instruction does and why it is useful in a Dockerfile.
Think about how commands run inside the container.
You got /4 concepts.
    Describe what happens when you use multiple WORKDIR instructions with relative paths in a Dockerfile.
    Consider how paths combine step by step.
    You got /4 concepts.

      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