LOGBOOK

HELP

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/app switches into that folder inside the image, creating it if it does not exist. Every later RUN, COPY, CMD and ENTRYPOINT uses it as the current directory, and so does a container's shell. Without WORKDIR, the starting point is /. It replaces fragile RUN cd … && … chains, since a cd in one RUN does not carry over to the next.
  • COPY <src> <dest> copies files or folders from the build context (the directory given to docker build) into the image. A relative destination such as . is resolved against WORKDIR.

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:

From Quiz: ISLAB / Docker Introduction | Updated: Nov 10, 2026