FROM alpine
WORKDIR /app
RUN pwdThe WORKDIR instruction sets the working directory for all subsequent commands in the Dockerfile. So, when the RUN command executes, the current directory is /app.
If the directory does not exist, Docker creates it automatically during the build process.
FROM ubuntu WORKDIR /app WORKDIR logs RUN pwd
What is the output of the RUN pwd command?
The second WORKDIR 'logs' is relative to the first '/app', so the final directory is '/app/logs'.
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?
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.
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.
