Which Dockerfile instruction would you use to set the initial working directory for subsequent instructions?

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

Which Dockerfile instruction would you use to set the initial working directory for subsequent instructions?

Explanation:
Setting the initial working directory for subsequent Dockerfile instructions is done with the WORKDIR instruction. It defines the directory that will be the current working directory for any following commands like RUN, COPY, or CMD. For example, choosing /app as the working directory means files copied later will go into /app, and commands run in RUN will execute with /app as the current directory. You can change it later by including another WORKDIR line, which sets a new current directory (absolute paths reset to that location, while relative paths are resolved against the previous one). The other instructions serve different purposes: FROM selects the base image, LABEL adds metadata, and CMD sets the default command to run when the container starts. CMD does not establish the working directory by itself, though it will run in whatever directory was set by a preceding WORKDIR.

Setting the initial working directory for subsequent Dockerfile instructions is done with the WORKDIR instruction. It defines the directory that will be the current working directory for any following commands like RUN, COPY, or CMD. For example, choosing /app as the working directory means files copied later will go into /app, and commands run in RUN will execute with /app as the current directory. You can change it later by including another WORKDIR line, which sets a new current directory (absolute paths reset to that location, while relative paths are resolved against the previous one).

The other instructions serve different purposes: FROM selects the base image, LABEL adds metadata, and CMD sets the default command to run when the container starts. CMD does not establish the working directory by itself, though it will run in whatever directory was set by a preceding WORKDIR.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy