Full lesson
Explore the full explanation, examples, and visuals at your own pace.
Dockerfile
Docker builds an image as a stack of layers. An image is a packaged snapshot of your app and everything it needs to run. A layer is one saved set of file changes. Docker reads this Dockerfile from top to bottom, and the lines that change files become layers.
FROM node:20-alpine
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
CMD ["node", "server.js"]Step 1: lines become layers
Docker builds from the bottom up. FROM node:20-alpine brings in the base layers, the starting files. COPY package.json adds a layer holding just that one file. Then RUN npm install adds a layer holding the node_modules folder it created. Each layer records only what its line changed, nothing more.
The image: myapp:1.0, read-only
The last line that changes files is COPY . ., which adds the app code layer on top. CMD only records the start command, so it adds no files. Now the stack is finished. Together, these four layers are the image, myapp:1.0, frozen as read-only and presented as one filesystem.
Container: a process plus a writable layer
When you run docker run, Docker starts node server.js as an isolated process, which is just a running program. It also adds a thin writable container layer on top of the image. Any file the app writes lands there. The image layers below stay untouched, and deleting the container removes that top layer.
Two containers, one shared image
An image is a stack of read-only layers: node:20-alpine, package.json, npm install, and app code. Container A and container B both sit on that same stack. Each adds only its own thin writable layer on top. So containers share the layers instead of copying them, and that's why they start fast.
Each layer is cached: same instruction, same files, same layer below
Docker reuses a saved layer only when three things match: the instruction is the same, the files it copies are the same, and every layer beneath it is unchanged.
Edit server.js: only the top rebuilds
Say you edit server.js. Walk down the layers. The base image node:20-alpine is cached. The package.json layer is cached too, and so is npm install. Only app code rebuilds, because server.js is copied by that last COPY line. So the slow install is skipped, and the rebuild takes seconds.
Edit package.json: everything above rebuilds
Now edit package.json instead. The node:20-alpine layer stays cached, but the package.json layer changes, so it rebuilds. Then npm install rebuilds, and so does app code, even though that line didn't change. Each layer sits on the one below it, so a change underneath invalidates everything above.
Line order decides what rebuilds
Look at what happens if code is copied first. Editing server.js invalidates COPY . ., and npm install sits above it, so the install reruns every time. With our order, the same edit only recopies the code, and the slow install stays cached. Put rarely changing lines first.
Images are stacked read-only layers; a container adds one writable top.
Images are stacked read-only layers, and a container adds one writable layer on top. Keep that picture in mind when you write a Dockerfile. Order your lines from least to most frequently changed, so an edit rebuilds only the top layers and everything below stays cached.