Module 3 Lesson 3: Docker Layers and Build Caching
·DevOps

Module 3 Lesson 3: Docker Layers and Build Caching

Build faster, not harder. Understand how Docker uses caching to skip unnecessary steps and learn how to order your Dockerfile instructions for maximum speed.

Module 3 Lesson 3: Docker Layers and Build Caching

Have you noticed that the first time you run docker build, it takes 2 minutes, but the second time it takes 2 seconds? That's the power of the Build Cache.

1. Everything is a Layer

In a Dockerfile, almost every instruction creates a new layer.

FROM alpine           # Layer 1
RUN apk add python3   # Layer 2
COPY . /app           # Layer 3

Docker stores each of these layers separately. If you don't change anything in Layer 2, Docker won't "Re-run" the install command; it simply reuses the cached layer from your disk.


2. Where the Cache Breaks (Invalidation)

Docker looks at each instruction from top to bottom.

  • If an instruction changes, that layer and ALL following layers are invalidated. Docker must rebuild them from scratch.
  • COPY Invalidation: If you run COPY . . and you have changed even one character in a file on your computer, Docker invalidates that layer.

Visualizing the Process

graph TD
    Start[Input] --> Process[Processing]
    Process --> Decision{Check}
    Decision -->|Success| End[Complete]
    Decision -->|Retry| Process

3. The "Optimization" Strategy

To build fast, you should put the things that change LEAST at the TOP of your Dockerfile.

The Wrong Way (Slow):

FROM python:3.9
COPY . /app
RUN pip install -r requirements.txt  # This runs EVERY time you change your code!
CMD ["python", "app.py"]

The Right Way (Fast):

FROM python:3.9
WORKDIR /app
# 1. Copy ONLY the requirements first
COPY requirements.txt .
# 2. Install dependencies (This layer is cached unless requirements.txt changes)
RUN pip install -r requirements.txt
# 3. NOW copy your changing source code
COPY . .
CMD ["python", "app.py"]

Result: Now, when you edit your code, Docker sees that requirements.txt hasn't changed, skips the pip install (which takes minutes), and only runs the COPY . . (which takes milliseconds).


4. Summary of Layer Caching

CauseEffect
Change FROMRebuilds everything
Change a command in RUNRebuilds that and all below
Edit a file used in COPYRebuilds that and all below
Nothing changed"Using Cache" (Instant)

Exercise: The Layer Optimization

  1. Look at this Dockerfile:
    FROM node:16
    COPY . /app
    RUN npm install
    CMD ["node", "index.js"]
    
    How would you rewrite it to ensure that npm install doesn't run every time you change a CSS file?
  2. If you change the version in FROM, why does it take so long to build?
  3. Why is it better to have one RUN apt-get update && apt-get install -y git rather than two separate RUN commands? (Hint: Think about cache invalidation of the second command).

Summary

Layer caching is the difference between a happy developer and a frustrated one. By strategically ordering your instructions and splitting your COPY commands, you can reduce your build times by 90% or more.

Next Lesson: Pro tips for production: Best practices for writing Dockerfiles.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn