Stage 1 · Foundations · Chapter 4 of 20 · 5 min read

Building images

How you build decides whether a deploy takes ten seconds or ten minutes: control the context and respect the cache.

? What do the diagram colours mean

key idea / Docker itselfcontainer / good pathhost / watch outproblem / blockedneutral

The build context

The dot in docker build . is the context: the folder that is uploaded to the daemon.

The build contextEverything in the build folder is sent to the daemon unless .dockerignore filters it out.Your folderFilterSent to the daemonsrc/Dockerfilepackage.jsonnode_modules/ 1 GB.git/.dockerignoredrops node_modules.git, local buildssrc/Dockerfilepackage.json
A 1 GB node_modules folder would travel to the daemon on every build.

The layer cache

Docker reuses any layer whose inputs did not change.

Layer cacheAfter hello.txt changes, the first two layers are cached and the COPY layer and everything after it rebuild.Build 1FROMbuiltWORKDIRbuiltCOPYbuiltRUNbuiltBuild 2 · hello.txt changedFROMCACHEDWORKDIRCACHEDCOPYfile changedRUNmust re-runCache is a time saver, until it hides a stale apt-get update: that is what --no-cache is for.
The cache breaks at the first changed layer. Everything below it re-runs.
Order your Dockerfile for the cacheCopying package.json and installing dependencies before copying source means a source edit does not reinstall packages.Slow: any edit reinstallsFast: install is cachedCOPY . .RUN npm ci (re-runs on every edit)COPY package*.json ./RUN npm ci (cached until deps change)COPY . . (only this re-runs)Rule of thumb: put what changes least at the top,and what changes most (your source) at the bottom.

Multi-stage builds

The tools you need to build an app (compilers, dev dependencies, source) are not the tools you need to run it. A multi-stage Dockerfile builds in one stage and copies only the result into a small final stage.

A multi-stage buildStage one starts from a full Node image, installs dependencies and builds the app. Stage two starts from a small nginx image and copies in only the built files, so the final image is small.Stage 1 · build (thrown away)FROM node:22 AS buildRUN npm ciRUN npm run builddist/ (the only part you need)Stage 2 · runtime (shipped)FROM nginx:1.27-alpinedist/ → /usr/share/nginx/htmlCOPY --from=build brings dist/ acrossnode_modules, compilers, sourceabout 1.1 GB, never shippedFinal imageabout 50 MB
Only the last stage becomes the image. Earlier stages are build scaffolding: they are cached, but never shipped.
FROM node:22 AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM nginx:1.27-alpine
COPY --from=build /app/dist /usr/share/nginx/html

The same pattern works for any compiled language: build a Go binary in golang:1.23, then copy just the binary into alpine or gcr.io/distroless/static. Smaller images pull faster and contain fewer packages to patch. Chapter 18 takes this further.

Flags worth knowing

Flag or fileEffect
-t name:1.0tag the image so you can find and run it
.dockerignorefilters the context: node_modules, .git, build output
--no-cacheignore the cache, rebuild every layer
--target buildstop at a named stage, for example to run tests in it
-f path/Dockerfileuse a Dockerfile that is not named Dockerfile

Try it yourself

Stay in the greet directory from the previous chapter, or recreate that Dockerfile first.

  1. Step 1 · See the context get sent

    A large unused file is still part of . until you ignore it.

    $ dd if=/dev/zero of=big.bin bs=1m count=20
    $ docker build -t greet:1 .
    # transferring context: 20MB
  2. Step 2 · Ignore it
    $ printf 'big.bin\n' > .dockerignore
    $ docker build -t greet:1 .
    # transferring context: a few kB
  3. Step 3 · Change a source file and watch the cache

    FROM and WORKDIR are reused. COPY and everything after it run again, because hello.txt changed.

    $ printf 'hello again\n' > hello.txt
    $ docker build -t greet:1 .
    [1/4] FROM docker.io/library/alpine:3.20   CACHED
    [2/4] WORKDIR /app                         CACHED
    [3/4] COPY hello.txt .
    [4/4] RUN printf 'built in the image\n' > /app/built.txt
  4. Step 4 · --no-cache rebuilds every layer
    $ docker build --no-cache -t greet:1 .
    [1/4] FROM docker.io/library/alpine:3.20
    [2/4] WORKDIR /app
    [3/4] COPY hello.txt .
    [4/4] RUN printf 'built in the image\n' > /app/built.txt
  5. Step 5 · A two-stage build

    Stage one installs a full compiler toolchain; stage two copies only the result into a fresh base.

    $ cat > Dockerfile.multi << 'EOF'
    FROM alpine:3.20 AS build
    RUN apk add --no-cache build-base && echo 'compiled!' > /out.txt
    FROM alpine:3.20
    COPY --from=build /out.txt /out.txt
    CMD ["cat", "/out.txt"]
    EOF
    $ docker build -f Dockerfile.multi -t multi:1 .
    $ docker run --rm multi:1
    compiled!
    $ docker images multi
    REPOSITORY   TAG   SIZE
    multi        1     7.8MB

    The compiler (over 200 MB) lived in stage one only. The final image is the size of plain Alpine.

Common mistakeForgetting .dockerignore. COPY . . then sends node_modules, .git and your .env into the build: slow builds, large images, and secrets saved in a layer.

Recap

Quick check

You edit one source file and the build reinstalls every npm package. What is likely wrong?

Show answer

Answer: The Dockerfile copies all the source before installing dependencies.

Copy package*.json, run the install, then copy the source, so the install layer stays cached when only source changes.

In a multi-stage build, what ends up in the final image?

Show answer

Answer: Only the last stage, plus anything it copies with COPY --from.

Only the final stage becomes the image. Earlier stages exist to produce files that COPY --from brings in.