Card 02/ 07

ExampleDifficulty: Intermediate1 min

The Same Build, Run Twice

Build once, change nothing, build again, and the output tells you exactly what it did.

bash
$ docker build -t my-api:1 .
 => [2/5] WORKDIR /app                      0.1s
 => [3/5] COPY package.json package-lock.json ./   0.0s
 => [4/5] RUN npm ci                        9.8s
 => [5/5] COPY . .                          0.1s
 => exporting to image                      1.2s
bash
$ docker build -t my-api:1 .
 => CACHED [2/5] WORKDIR /app
 => CACHED [3/5] COPY package.json package-lock.json ./
 => CACHED [4/5] RUN npm ci
 => CACHED [5/5] COPY . .
 => exporting to image                      0.3s

CACHED on every step. The nine seconds of npm ci were not spent again, because the instruction and the layer underneath it were both unchanged.