Challenge - 5 Problems
WORKDIR Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Effect of WORKDIR on RUN command
Given the Dockerfile snippet below, what will be the current directory when the RUN command executes?
Docker
FROM alpine
WORKDIR /app
RUN pwdAttempts:
2 left
💡 Hint
WORKDIR sets the working directory for following instructions.
✗ Incorrect
The WORKDIR instruction sets the working directory for all subsequent commands in the Dockerfile. So, when the RUN command executes, the current directory is /app.
🧠 Conceptual
intermediate2:00remaining
WORKDIR creates directories if missing
What happens if the directory specified in WORKDIR does not exist in the image?
Attempts:
2 left
💡 Hint
Think about how Docker handles paths that don't exist.
✗ Incorrect
If the directory does not exist, Docker creates it automatically during the build process.
❓ Configuration
advanced2:00remaining
Multiple WORKDIR instructions effect
Consider this Dockerfile snippet:
What is the output of the RUN pwd command?
FROM ubuntu WORKDIR /app WORKDIR logs RUN pwd
What is the output of the RUN pwd command?
Attempts:
2 left
💡 Hint
Each WORKDIR is relative to the previous one if not absolute.
✗ Incorrect
The second WORKDIR 'logs' is relative to the first '/app', so the final directory is '/app/logs'.
❓ Troubleshoot
advanced2:00remaining
Unexpected directory in container
A developer writes this Dockerfile:
But when running the container, files are missing in /usr/src/app. What could cause this?
FROM node:18 WORKDIR /usr/src/app COPY . . RUN npm install CMD ["node", "server.js"]
But when running the container, files are missing in /usr/src/app. What could cause this?
Attempts:
2 left
💡 Hint
COPY uses the build context root, not WORKDIR, as source.
✗ Incorrect
The COPY command copies files from the build context root to the destination path. The destination '.' is relative to WORKDIR, but the source '.' is always relative to the build context root.
✅ Best Practice
expert3:00remaining
Optimizing Dockerfile with WORKDIR
Which Dockerfile snippet best follows best practices for setting WORKDIR and copying files to reduce image layers and improve readability?
Attempts:
2 left
💡 Hint
Consider layer caching and clarity of commands.
✗ Incorrect
Option C sets WORKDIR early, copies only requirements.txt first to leverage caching, installs dependencies, then copies the rest. This reduces rebuild time and improves readability.