Lesson 2 · Unit 02
The build cache
Why one changed line can rebuild everything after it
What you practise
- Each instruction is a layer, a change invalidates everything after it, copying the dependency manifest first,
.dockerignore
Try a question from this lesson
Pick one
A build takes 3 minutes, nearly all of it installing packages. You fix a typo in one source file and build again. What could a careful builder safely skip?
Correct answer
Every step whose instruction and input files are unchanged
Why
A step with the same instruction and the same inputs gives the same result, so the saved layer can be reused. That is the build cache. The catch, coming next, is what counts as an input.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Reuse until the first change
Docker checks the instructions in order. If a step and its input files match the previous build, its saved layer is reused.
The first step that differs is rebuilt, and so is every step after it, because each layer is built on top of the one before.
=> CACHED [1/4] FROM python:3-slim
=> CACHED [2/4] WORKDIR /app
=> [3/4] COPY . .
=> [4/4] RUN pip install ...Concept
Rarely changed first
Order the steps from what changes least to what changes most.
Dependencies change far less often than code. So copy only the dependency list, install, and copy the rest of the project afterwards. A code edit then invalidates just the last, fast copy.
changes rarely base image
dependency list
installed packages
changes often your codeConcept
.dockerignore
Before a build starts, the whole build context is sent to the builder. A file named .dockerignore in the root of the context lists patterns to leave out.
Ignored files are never sent, cannot be copied into the image by accident, and cannot invalidate the cache when they change.
.git
node_modules
*.log
.env9 more exercises in the app
- 4× multiple choice
- 2× type the answer
- 1× fill the blanks
- 1× true or false
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.