Courseiva

CKAD Application Design and Build Practice Question

A developer is building an image for a Node.js application. The Dockerfile currently copies package.json and package-lock.json, runs npm ci, then copies the rest of the source code. They want to ensure that rebuilding the image after only editing application source files does not re-run npm ci. Which statement about Docker layer caching explains why this Dockerfile ordering achieves that goal?

⚠ Common exam trap

The trap here is assuming Docker understands package managers or caches command results globally, rather than caching layers keyed by instruction and file content.

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

✓

Each Dockerfile instruction creates a layer; a layer is reused from cache if its instruction and the contents of files it references are unchanged, so npm ci is only invalidated when the dependency manifests change.

Docker layer caching reuses a layer when its instruction and referenced file contents match the previous build. Copying dependency manifests first and running npm ci before copying source code means source edits do not invalidate the dependency layer, so npm ci is skipped and rebuilds are faster.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    Docker caches the result of every RUN instruction globally in the local image store keyed by the command string, so npm ci is skipped whenever the same command appears in any Dockerfile on the host.

    Why it's wrong here

    RUN cache entries are not global across unrelated Dockerfiles; they are tied to the parent layer chain and the exact instruction. A different Dockerfile or a changed earlier layer produces a different cache key, so the npm ci result would be recomputed rather than reused from an unrelated build.

  • ✗

    The COPY instruction always invalidates all subsequent layers, so placing COPY package.json before npm ci forces npm ci to run only when the source code changes.

    Why it's wrong here

    COPY invalidates only itself and layers after it when the copied content changes. Placing COPY package.json earlier actually protects the npm ci layer from source changes; the claim that COPY always invalidates all subsequent layers is false and would defeat the intended caching behavior.

  • ✗

    Docker automatically detects that npm ci is a dependency installation step and caches its output separately from the image layers, independent of Dockerfile instruction order.

    Why it's wrong here

    Docker has no semantic awareness of npm ci or any package manager. It does not cache command output separately from layers, so the layer ordering in the Dockerfile is exactly what determines whether the dependency installation step is reused or rerun.

  • ✓

    Each Dockerfile instruction creates a layer; a layer is reused from cache if its instruction and the contents of files it references are unchanged, so npm ci is only invalidated when the dependency manifests change.

    Why this is correct

    Docker builds images layer by layer, and a cached layer is reused when the instruction text and the copied file contents are identical. Since package.json and package-lock.json are copied before the source, editing only source files does not invalidate the earlier npm ci layer, so dependencies are not reinstalled.

About these practice questions

This CKAD question is part of Courseiva's 826-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 →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official CNCF exam blueprint

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.