DockerDockerfileLinuxIntermediate~4h

Docker: Images & Layers

Build an image you can reason about

An image is a stack of layers, and almost every slow build is somebody who does not know which ones a change rebuilds. That has a right answer, and it is computable. This is the whole build side: the cache rule, the context, what a COPY puts where, what an image weighs, and what the container actually runs.

Modules
5
Drills
0
Build steps
4
Time
~4h

Walk out able to

Look at a Dockerfile and a change and say which instructions re-run — then reorder it so the slow one stops.

What you work on

01

Images and their layers

What a container adds, and which lines make a layer.

02

The cache rule

Work out exactly which instructions a change re-runs.

03

Context, size and stages

What gets sent, what ships, what a second stage drops.

04

What the container runs

ENTRYPOINT, CMD, and the arguments that override them.

The pitch

What you practise, and what you leave with

A multi-stage Dockerfile of your own, ordered so a code change re-runs no install — plus the two build times and two image sizes that prove it.

You will practise

  • Images, containers and what makes a layer
  • Which instructions a change re-runs
  • Build context, .dockerignore and COPY
  • Image size, stages, ENTRYPOINT and CMD

Afterwards you can

  • Count the instructions a change rebuilds
  • Reorder a Dockerfile so the cache holds
  • Say what a delete does to image size
  • Predict the argv a container runs

Modules

5 modules, 21 items

Lessons explain one idea. Drills repeat it until it sticks. Build steps make something that exists afterwards.

  1. 1

    What is in an image

    ~35 min

    Say what a container adds to an image, and which lines of a Dockerfile make a layer.

    • LessonWhy build an image at all5 min
    • LessonRun one image twice, watch it diverge8 min
    • LessonSee what survives a stop and a remove9 min
    • LessonSpot the lines that cost you anything13 min
  2. 2

    Make the cache work for you

    ~50 min

    Work out which instructions a change re-runs, then reorder the file so the slow one stops.

    • LessonFind the first instruction that misses9 min
    • LessonCount what a change actually re-runs10 min
    • LessonReorder a Dockerfile so the cache holds9 min
    • LessonPut a number on the second build8 min
    • Build stepStamp the layers and build twice14 min
  3. 3

    Send only what is needed

    ~40 min

    Say what the builder receives, what `.dockerignore` keeps out, and where a `COPY` lands.

    • LessonSee what the builder is handed8 min
    • LessonRead a .dockerignore line by line9 min
    • LessonLand a COPY where you meant to12 min
    • Build stepCatch a .dockerignore that does nothing11 min
  4. 4

    Ship a smaller image

    ~43 min

    Account for an image layer by layer, and split the build so the toolchain never ships.

    • LessonDelete a file and make the image bigger14 min
    • LessonSplit the build into two stages8 min
    • LessonDecide what crosses the boundary8 min
    • Build stepBuild it in two stages and look inside13 min
  5. 5

    Decide what it runs

    ~51 min

    Assemble the argv from `ENTRYPOINT`, `CMD` and run arguments, then write one yourself.

    • LessonTell the exec form from the shell form8 min
    • LessonWork out what a run argument replaces9 min
    • Build stepWatch a shell form swallow an argument12 min
    • LessonWrite a Dockerfile from a specification22 min

A taste of the code

Dockerfile
FROM python:slim-bookworm
WORKDIR /app

# Dependencies first: they change rarely, so this layer stays cached.
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

# Source last: it changes every commit, and nothing below it is slow.
COPY src/ ./src/

ENTRYPOINT ["python", "-m", "src.report"]
CMD ["--format", "table"]

$ docker run reports --format csv

rows=128 format=csv

Stop waiting four minutes for a one-line change.

The first item is free. ~4h of focused work, at your own pace.

Intermediate~4h5 modules