Module 3 · Project 03 · Dockerize a Real Application · Lesson 12 of 48

Layers, the build cache and lean, secure images

How layers and caching work, and how multi-stage builds, small base images and a non-root user make images faster, smaller and safer.

In this lesson · about 30 minutes

  • Explain image layers and the build cache
  • Order Dockerfile instructions for fast rebuilds
  • Use multi-stage builds, a non-root user, pinned versions and a health check
  • Explain CMD vs ENTRYPOINT

Every instruction is a layer

Each FROM, COPY and RUN creates a layer: a saved snapshot of the files it added or changed. An image is a stack of layers. Layers are shared, so ten images built on node:22-alpine store that base only once.

The build cache

When you rebuild, Docker reuses a saved layer if the instruction and everything before it are unchanged. As soon as one layer changes, every layer after it is rebuilt. That's why the Dockerfile copied package*.json and ran npm ci before copying the code: you edit code all day but change dependencies rarely, so the slow install stays cached.

Smaller and safer images

  • Small base images. node:22 is over 1 GB; node:22-alpine or a "slim" or "distroless" image is a fraction of that. Less to download and fewer packages that could have vulnerabilities.
  • Multi-stage builds. Use one stage with all the build tools to compile, then copy only the finished result into a clean final stage. The build tools never reach production.
  • Don't run as root. Add USER node (or create a user). If someone breaks into the app, they aren't root in the container.
  • Pin versions. node:22.11-alpine or even an image digest, and a lockfile with npm ci, so builds are repeatable.
  • Add a health check. HEALTHCHECK lets Docker (and later Compose, ECS or Kubernetes) know whether the app actually works, not just whether the process exists.

Try it · watch it run

Build an image, layer by layer

Build the simple Dockerfile, then the optimized one. Try each kind of change and compare build time, image size and the user it runs as.

Final image size

1291 MB

This build

55s

Runs as

root

FROM node:22
WORKDIR /app
COPY . .
RUN npm install
CMD ["node", "server.js"]
FROM node:2212s · 1100 MB

Layers, bottom up

Step 1 of 5FROM node:22: download the base image (1100 MB). Every image starts from one.

The optimized Dockerfile

# ---- build stage: has dev tools, thrown away at the end
FROM node:22.11-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build && npm prune --omit=dev

# ---- final stage: only what production needs
FROM node:22.11-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build /app/dist ./dist
COPY --from=build /app/node_modules ./node_modules
USER node
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s CMD wget -qO- localhost:3000/health || exit 1
CMD ["node", "dist/server.js"]

CMD vs ENTRYPOINT

Both say what runs when a container starts. CMD is a default you can replace: docker run notes-app:1.0 node migrate.js runs the migration instead. ENTRYPOINT is the fixed program, and anything after the image name becomes its arguments. A common pattern is ENTRYPOINT ["node"] with CMD ["dist/server.js"]. Use the JSON (exec) form shown here, so your app receives stop signals directly and shuts down cleanly.

Whiteboard · watch and try

Why order matters for the cache

Press Play to see two orderings of the same three steps.

5 steps
Press Play and the tutor will talk you through it while writing on the board. Pause any time to write on the board yourself.

Tip: press Pause, then use the pen to work along on the board. Space plays and pauses; Ctrl+Z undoes your last mark.

Sorting drill

Smaller, safer or neither?

Sort each Dockerfile habit.

  • FROM node:22-alpine instead of node:22
  • Running the app as root
  • COPY .env into the image
  • A multi-stage build
  • FROM node:latest
  • A HEALTHCHECK that calls /health
  • A .dockerignore with node_modules and .git

0 of 7 sorted

Check your understanding0 of 3 correct

Question 1

1.You change one line of app code. Which layers does Docker rebuild?

Question 2

2.Why use a multi-stage build?

Question 3

3.Image has CMD ["node", "server.js"]. What does docker run img node migrate.js do?

Key terms

Say the meaning to yourself, then tap a card to check.

Quiz score: 0 of 3

Tip: answer every quiz question correctly first.

Written by Noviq Academy in its own words. The labs are simulations: no real servers are touched. When you build the projects yourself, use a practice virtual machine or a free-tier cloud account you control, watch your cloud spending, and never run cleanup or delete commands on a machine you care about. Tool names belong to their owners; versions and output are shown for learning and may differ slightly from yours. Read-aloud and the whiteboard voice use your device's built-in voices.