What if you could stop repeating long folder paths and avoid silly mistakes in your Docker builds?
Why WORKDIR instruction for directory in Docker? - Purpose & Use Cases
Imagine you are building a Docker image and need to run several commands inside a specific folder. Without setting a working directory, you must type the full path every time you want to access files or run commands there.
This manual way is slow and error-prone because you have to repeat long paths in every command. It's easy to make typos or forget to change directories, causing build failures or unexpected results.
The WORKDIR instruction sets a default directory inside the container. After setting it once, all following commands run inside that folder automatically, making your Dockerfile cleaner and less error-prone.
RUN mkdir -p /app && cd /app && touch file.txt RUN cd /app && ls
WORKDIR /app RUN touch file.txt RUN ls
It enables simpler, clearer Dockerfiles where commands naturally run in the right folder without repeating paths.
When building a web app image, you can set WORKDIR to the app folder so all build steps like installing dependencies and copying files happen inside that folder automatically.
Manually changing directories in Dockerfiles is repetitive and error-prone.
WORKDIR sets a default folder for all following commands.
This makes Dockerfiles easier to read and maintain.
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
