University information

Order Dockerfile Layers Around Dependency Changes

In this guide +

A small application change should not be confused with a change to every dependency it uses. This workshop compares two Dockerfile layouts and asks you to trace which later build steps become candidates for rebuilding. The aim is to understand the dependency boundary, not to claim that a particular machine or registry has a warm cache.

Start with the documented build model

Docker’s Dockerfile guide describes the roles of instructions such as FROM, WORKDIR, COPY and RUN.

Docker’s cache guide explains how instruction order and unchanged input files can permit reuse of earlier build layers.

You need basic familiarity with files, a dependency list and command-line programs. A Docker installation is optional for the paper exercise. The examples are conceptual; replace placeholder commands and the base image with valid choices before any local execution. No image is downloaded or service deployed by reading this guide.

Compare two original instruction plans

Same intended application, different dependency boundaries
Step Plan A: narrow early copy Plan B: copy everything early
1 Choose base and work directory Choose base and work directory
2 Copy requirements.txt Copy all project files
3 Install dependencies Install dependencies
4 Copy only the src application directory Set application command
5 Set application command End

Assume the base and dependency file are unchanged and the cache remains available. Then change only a line in app.py. In Plan A, the early dependency-file copy has the same input, so the reasoning boundary keeps that work separate from the changed application copy. In Plan B, the early all-files copy includes app.py, so the change occurs before dependency installation.

Trace the affected region

Application-only change:
Plan A: base → requirements → install | app copy → later work
Plan B: base | all-files copy → install → later work

Dependency-list change:
Plan A: base | requirements → install → app copy → later work
Plan B: base | all-files copy → install → later work

The vertical bar marks the first changed-input boundary in this simplified comparison. It is not a measured runtime or a guarantee that every earlier result will be reused. Cache availability and other build inputs still matter. The important answer is the dependency relationship: Plan A gives an application-only edit a later boundary.

When requirements.txt changes, both plans have a reason to revisit installation. Moving instructions cannot make a changed dependency specification irrelevant. An argument that Plan A “never reinstalls packages” is therefore false even in this small model.

Now change only docs/readme.md, which Plan A never copies. That file does not alter either of Plan A’s declared copy inputs, while Plan B’s all-files copy includes it. This third case tests whether the review follows actual input boundaries instead of assuming every project change has the same effect.

Distinguish efficient reuse from reproducibility

A build can reuse cached work yet remain dependent on changing external inputs. Conversely, a deliberately fresh build can reproduce a declared result. Write down the base-image reference, dependency specification and inputs whose stability you are assuming. Do not treat a fast second run as proof that the image can be recreated elsewhere.

For a second scenario, keep the application unchanged but change the base image. Both plans depend on that earlier choice. The later copy order does not isolate the application from the base. This is why the table starts with a common first step rather than presenting dependency installation as the beginning of the whole build.

Prepare a small review

Produce two annotated instruction lists and answer three questions: what changed, which step first consumes that change, and which assumptions make earlier reuse plausible? Keep performance claims separate. If you execute a local build, report the actual environment and observation rather than inserting a universal percentage improvement.

The notebook reproducibility workshop examines declared inputs and stale state. The command-line application workshop provides a related application boundary to think about. Find further exercises in the workshop collection.

Questions about the comparison

Does Plan A always build faster?

No timing guarantee is made. It separates inputs in a way that can permit reuse under the stated cache assumptions; actual performance needs measurement.

What changes when requirements.txt changes?

The dependency-copy input changes, so installation and subsequent work fall after the affected boundary in Plan A.

Is cache reuse proof of a reproducible image?

No. A reproducibility claim also needs declared and controlled inputs. A previous cached result can conceal changes outside the small model.

Official reference

Checked 17 October 2026: Dockerfile instructions and cache guidance. The three-change comparison is original.

Related guides

View the worked-example diagram
Three file changes affect different stages of narrow-copy and all-files Dockerfile plans.
Original explanatory diagram; consult the linked official guidance for current requirements.

Privacy choices

Allow Google Ads measurement cookies and use of ad-click information to attribute visits to our public information pages? This optional measurement stays off until you accept. Separate analytics, personalised advertising and enhanced conversions remain off; we do not configure form-field collection. This new attribution permission is not covered by your earlier choice. You can refuse or change it at any time.

Read our privacy policy