0
0
Dockerdevops~20 mins

WORKDIR instruction for directory in Docker - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WORKDIR Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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 pwd
A/app
B/
C/root
D/usr/local/bin
Attempts:
2 left
💡 Hint
WORKDIR sets the working directory for following instructions.
🧠 Conceptual
intermediate
2:00remaining
WORKDIR creates directories if missing
What happens if the directory specified in WORKDIR does not exist in the image?
ADocker creates the directory automatically
BDocker throws an error and stops building
CDocker uses the root directory instead
DDocker ignores the WORKDIR instruction
Attempts:
2 left
💡 Hint
Think about how Docker handles paths that don't exist.
Configuration
advanced
2:00remaining
Multiple WORKDIR instructions effect
Consider this Dockerfile snippet:
FROM ubuntu
WORKDIR /app
WORKDIR logs
RUN pwd

What is the output of the RUN pwd command?
A/logs
B/app/logs
C/app
D/
Attempts:
2 left
💡 Hint
Each WORKDIR is relative to the previous one if not absolute.
Troubleshoot
advanced
2:00remaining
Unexpected directory in container
A developer writes this Dockerfile:
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?
ACOPY command copies files relative to the current WORKDIR
BCMD command changes the working directory
CWORKDIR must be set after COPY to affect file locations
DCOPY command copies files relative to the build context root
Attempts:
2 left
💡 Hint
COPY uses the build context root, not WORKDIR, as source.
Best Practice
expert
3: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?
A
FROM python:3.12
RUN mkdir /app
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
B
FROM python:3.12
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
C
FROM python:3.12
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
D
FROM python:3.12
COPY requirements.txt /app/
RUN pip install -r /app/requirements.txt
WORKDIR /app
COPY . .
Attempts:
2 left
💡 Hint
Consider layer caching and clarity of commands.