Lesson 2.2

Layer caching and build speed

The builder reuses a cached layer until one instruction changes; from that point everything after it rebuilds.

14mBeginner23.2k students

Overview

Put the things that change last, last

The builder reuses a cached layer until one instruction changes; from that point everything after it rebuilds. So the order of your instructions is the single biggest factor in build time.

The standard pattern is to copy only the dependency manifest, install dependencies, and then copy the source. A code change then invalidates only the final layers, and the dependency install — the slow part — is reused.

Copying the whole project before installing defeats this entirely: every one-character change reinstalls everything.

In this lesson you will:

  • Order instructions from least to most volatile
  • Copy dependency manifests before source
  • Recognise what invalidates the cache

Resources

Previous Lesson
Next Lesson
Layer caching and build speed — Docker Essentials — Vertex