Quiz Entry - updated: 2026.11.10
What do the Dockerfile instructions WORKDIR and COPY do?
WORKDIR sets (and creates if needed) the working directory for all following instructions; COPY copies files from the build context into the image, relative to that directory.
WORKDIR /usr/src/app
COPY package.json .
RUN npm install
COPY . .
CMD ["node", "app.js"]
WORKDIR /usr/src/appswitches into that folder inside the image, creating it if it does not exist. Every laterRUN,COPY,CMDandENTRYPOINTuses it as the current directory, and so does a container's shell. WithoutWORKDIR, the starting point is/. It replaces fragileRUN cd … && …chains, since acdin oneRUNdoes not carry over to the next.COPY <src> <dest>copies files or folders from the build context (the directory given todocker build) into the image. A relative destination such as.is resolved againstWORKDIR.
ADD is similar but also unpacks local tar archives and can fetch URLs. Because of that hidden behaviour, COPY is preferred unless you need it.
The example also shows the cache trick: copy only the dependency list first and install, then copy the rest of the code. Code changes then don't invalidate the slow npm install layer.
Go deeper:
Docker Docs: Dockerfile reference, WORKDIR — relative paths and how WORKDIR applies to later instructions.
Docker Docs: Building best practices, ADD or COPY — why COPY is preferred.