Bird
Raised Fist0
Dockerdevops~15 mins

WORKDIR instruction for directory in Docker - Mini Project: Build & Apply

Choose your learning style10 modes available
Using WORKDIR Instruction in Dockerfile
📖 Scenario: You are creating a Docker image for a simple web application. You want to organize your files inside the container by setting a working directory.
🎯 Goal: Learn how to use the WORKDIR instruction in a Dockerfile to set the working directory for subsequent commands.
📋 What You'll Learn
Create a Dockerfile with a base image
Set a working directory using WORKDIR
Add a file inside the working directory
Print the current working directory inside the container
💡 Why This Matters
🌍 Real World
Setting a working directory in Docker images is common to keep files organized and commands simple.
💼 Career
Understanding <code>WORKDIR</code> is essential for writing clean Dockerfiles used in software development and deployment.
Progress0 / 4 steps
1
Create a Dockerfile with base image
Create a Dockerfile and write the line FROM alpine:latest to use the Alpine Linux base image.
Docker
Hint

The FROM instruction sets the base image for your Dockerfile.

2
Set the working directory
Add the line WORKDIR /app to set the working directory inside the container to /app.
Docker
Hint

The WORKDIR instruction sets the directory where commands will run inside the container.

3
Add a file inside the working directory
Add the line RUN echo "Hello from /app" > message.txt to create a file named message.txt inside the /app directory.
Docker
Hint

The RUN instruction executes commands inside the container. Here it creates a file in the working directory.

4
Print the current working directory
Add the line CMD ["pwd"] to print the current working directory when the container runs.
Docker
Hint

The CMD instruction defines the command to run when the container starts.

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