What does the WORKDIR instruction do in a Dockerfile?

Prepare for the KCNA Certification Test. Study with flashcards, multiple-choice questions, and detailed explanations to enhance your understanding of Kubernetes Cloud Native concepts. Ace your exam!

Multiple Choice

What does the WORKDIR instruction do in a Dockerfile?

Explanation:
WORKDIR defines the directory that becomes the current working directory for any following instructions in the Dockerfile. If that directory doesn’t exist yet, Docker will create it (including any needed parent directories) so later steps like RUN, COPY, ADD, CMD, or ENTRYPOINT operate from that location. For example, setting WORKDIR /app means subsequent commands run with /app as the base, and a COPY would place files into /app by default. This can be chained to move into subdirectories, with each new path created if missing. The other options refer to unrelated behaviors: one suggests no directory creation, another describes entrypoint, and the last describes port exposure. The described behavior—setting and ensuring the directory exists for subsequent instructions—is the correct understanding of WORKDIR.

WORKDIR defines the directory that becomes the current working directory for any following instructions in the Dockerfile. If that directory doesn’t exist yet, Docker will create it (including any needed parent directories) so later steps like RUN, COPY, ADD, CMD, or ENTRYPOINT operate from that location. For example, setting WORKDIR /app means subsequent commands run with /app as the base, and a COPY would place files into /app by default. This can be chained to move into subdirectories, with each new path created if missing. The other options refer to unrelated behaviors: one suggests no directory creation, another describes entrypoint, and the last describes port exposure. The described behavior—setting and ensuring the directory exists for subsequent instructions—is the correct understanding of WORKDIR.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy