CKAD Application Design and Build Practice Question
A developer wants to containerize a Node.js application. The Dockerfile should first copy only package.json and package-lock.json, run npm install, then copy the rest of the source code. Which Dockerfile best achieves this?
⚠ Common exam trap
In the CKAD exam, candidates often mistakenly use `ADD` instead of `COPY`, but `COPY` is the recommended command for copying local files to a Docker image without unnecessary side effects. The exam emphasizes efficient layer caching, so using `COPY` for local files and separating dependency installation from source code copying is a best practice.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
COPY package*.json /app/\nRUN npm install\nCOPY . /app/
It first copies only package.json and package-lock.json (using a wildcard pattern), runs `npm install` to leverage Docker's layer caching, and then copies the rest of the source code. This ensures that subsequent builds only re-run `npm install` when the dependency files change, not on every source code modification, which is a best practice for efficient Docker builds.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
COPY . /app\nRUN npm install
Why it's wrong here
Placing `COPY . /app` before `RUN npm install` means every source file edit invalidates the layer containing the application code, forcing Docker to re-run the entire npm install on each build even when `package.json` and `package-lock.json` are unchanged. This discards the dependency layer cache that makes subsequent builds fast. The solution is to copy only the package manifests first, install dependencies, and only then copy the rest of the source.
- ✗
ADD package*.json /app/\nRUN npm install\nADD . /app/
Why it's wrong here
This Dockerfile uses the correct layer ordering (manifests first, install next, source later) but applies the wrong instruction: `ADD` is designed for remote URLs and automatic tar archive extraction, not for plain local file copies. When the second `ADD . /app/` runs, any archive present in the build context is silently expanded into the image, leading to an unexpected filesystem layout. `COPY` is the explicit, side-effect-free command for copying local files and should be used instead.
- ✓
COPY package*.json /app/\nRUN npm install\nCOPY . /app/
Why this is correct
This is the recommended pattern: copying only `package*.json` first makes the `RUN npm install` layer depend solely on dependency manifests, so it remains cached unless those files change. After install, the remaining application code is copied in a separate layer, letting source-code edits rebuild quickly without reinstalling dependencies. Using `COPY` for both operations is correct for local build-context files, and if the source contains a `node_modules` directory, a `.dockerignore` entry should exclude it to avoid overwriting the freshly installed dependencies.
- ✗
ADD . /app\nRUN npm install
Why it's wrong here
Using `ADD . /app` before `RUN npm install` repeats the cache-order mistake of copying the whole source tree too early, so any modification invalidates the npm install layer and forces a full dependency rebuild. Additionally, `ADD` brings its extra semantics: automatic extraction of local tarballs and support for remote URL fetching, both of which are likely unintended in a simple Node.js build. A safe, efficient approach is to `COPY` package manifests, run `npm install`, then `COPY` the remaining source.
Go deeper
Related to this question
About these practice questions
This CKAD question is part of Courseiva's 160-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This CKAD practice question is part of Courseiva's free CNCF certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the CKAD exam.