Docker
/Intermediate
Build Cache
Definition
Docker's mechanism for dramatically speeding up the image building process by saving the result of every individual Dockerfile instruction as a reusable layer on the hard drive.
Explain Like I'm New
Docker takes notes. If step 2 is 'Install Python', Docker does it once and saves it. If you build the image again tomorrow, Docker says 'I already did step 2 yesterday', instantly skips it, and moves to step 3.
Real World Example
A developer changes one line of CSS in their app. Because they structured their Dockerfile correctly, Docker instantly uses the cached layers for installing Ubuntu, Node, and Webpack, and ONLY spends 2 seconds rebuilding the final CSS layer.
Common Use Cases
- •Rapid prototyping
- •CI/CD optimization
Interview Questions
basic
- If you change a command on line 3 of your Dockerfile, will Docker still use the cached layer for line 4?
intermediate
- Why do developers always run `COPY package.json` BEFORE they run `COPY . .` in Node.js Dockerfiles?