devops / containers
Docker for DevOps: Write a Dockerfile, Master Layer Caching, Shrink the Image
A working multi-stage Dockerfile for a Node.js app, built and run for this page, with the real cache output and image sizes. Then when Kubernetes becomes worth learning, and which free labs are still online.
A container image is a stack of read-only layers plus a start command. Docker builds that stack from a Dockerfile, one layer per instruction, and reuses any layer whose inputs have not changed. Almost every practical Docker skill follows from those two facts: your instruction order decides how fast rebuilds are, and the contents of your layers decide how big the image is. Most "learn Docker" pages stop at docker run hello-world. This one starts from a Dockerfile you would be comfortable shipping and explains each decision. The numbers below come from building it on a Windows machine with Docker 25.0.3 in October 2026, so they are measurements, not estimates.
01 · THE DOCKERFILE
A multi-stage Dockerfile you can reuse
This builds a Node.js app in one stage and ships only what it needs to run in a second stage. It was built and executed against a tiny demo project (a package.json, a lockfile, and a build script that copies src/server.js to dist/).
# syntax=docker/dockerfile:1 FROM node:22-slim AS build WORKDIR /app COPY package.json package-lock.json ./ RUN npm ci COPY . . RUN npm run build FROM node:22-slim WORKDIR /app ENV NODE_ENV=production COPY package.json package-lock.json ./ RUN npm ci --omit=dev COPY --from=build /app/dist ./dist USER node CMD ["node", "dist/server.js"]
| Instruction | Reason |
|---|---|
FROM node:22-slim AS build | Names the first stage so the second can copy files out of it. The slim variant is a smaller Debian-based image. |
COPY package.json package-lock.json before COPY . . | Dependencies change rarely and source changes constantly, so the slow install gets its own layer ahead of the volatile one. |
RUN npm ci | Installs exactly the lockfile's versions, so the image matches what you tested. |
Second FROM | Starts again from a clean base. Everything from the build stage that you do not copy across, such as dev dependencies and caches, is left behind. |
npm ci --omit=dev | Installs production dependencies only in the final image. |
COPY --from=build | Pulls just the built output out of the first stage. |
USER node | Runs the app as the node user instead of root. Running id in the built image printed uid=1000(node). |
Pair it with a .dockerignore file that lists node_modules, dist, .git and *.log. Docker's documentation describes .dockerignore as the way to exclude files from the build context, which keeps the context small and stops a local node_modules from overwriting the one installed in the image.
02 · LAYER CACHING
Why a one-line code change should not reinstall your packages
Docker's cache-invalidation rules say the builder compares each instruction to the cached layers in order, and when no cached layer matches an instruction exactly, the cache is invalidated from that point on. Everything after a changed layer is rebuilt. So the position of COPY . . decides what a source edit costs.
To check it, the demo image was built once, then src/server.js was edited and the image rebuilt. The plain build log for the second run read:
[build 2/6] WORKDIR /app CACHED [build 3/6] COPY package.json package-lock.json CACHED [build 4/6] RUN npm ci CACHED [build 5/6] COPY . . DONE 0.0s [build 6/6] RUN npm run build DONE 0.6s [stage-1 4/5] RUN npm ci --omit=dev CACHED [stage-1 5/5] COPY --from=build /app/dist ./dist DONE 0.1s
The edit touched layer 5 onward, and both npm ci steps were reused. If the Dockerfile had put COPY . . before the install, the same edit would have changed that layer and forced a full reinstall every time. On a real project the install is the slow part, so this one ordering habit is worth more than any other Dockerfile tuning.
RUN apt-get update or a floating base tag stays cached until its own text changes. Docker's best-practices guide notes that a tag such as ubuntu:24.04 can resolve to a different image over time, and pins by digest when you need an exact, repeatable base. Rebuild with --no-cache when you suspect an old layer is being reused.03 · IMAGE SIZE
From 1.13 GB to 228 MB: what actually shrinks an image
The same demo app was built two ways: a single-stage file on the full node:22 image using npm install, and the two-stage file above. Sizes are from docker images on the local machine, uncompressed.
| Build | Image size | What changed |
|---|---|---|
Single stage, FROM node:22, npm install | 1.13 GB | Full Debian base with compilers and build tools, dev dependencies and source all kept |
Two stages, FROM node:22-slim, npm ci --omit=dev | 228 MB | Slim base, production dependencies only, built output only |
Most of that gap is the base image. Docker Hub's tag page for the official Node image listed the linux/amd64 compressed size of node:trixie-slim at 81.77 MB and node:trixie at 424.19 MB when checked. Docker's best-practices guide says to choose a minimal base that matches your needs, and to use one image for building and another, usually slimmer, for production. Alpine is under 6 MB according to that guide, though a very small base also means fewer tools for debugging. The distroless project goes further and ships language runtimes without an operating system shell or package manager.
To see where bytes go, run docker history your-image. In the demo, the production npm ci layer was 1.34 MB and the copied build output was 28 bytes; the base image was nearly everything. The dive tool shows the same breakdown interactively, layer by layer.
04 · WHEN TO ADD KUBERNETES
Docker alone, Compose, or Kubernetes
Docker builds and runs containers on one machine. Kubernetes runs containers across many machines and keeps them in the declared state: restarting failures, rolling out new versions, and spreading load. That power has a learning cost, so match the tool to the problem.
| Your situation | Reasonable choice |
|---|---|
| One app, one server, a few services | Docker with Compose on that server |
| A team deploying many services with zero-downtime releases | Kubernetes, usually a managed one from a cloud provider |
| Learning how clusters work | A local cluster such as minikube, and the official tutorials below |
| A job listing that says "Kubernetes" | Learn Docker first; the cluster concepts assume you understand images and containers |
05 · STUDY MATERIAL
Free tutorials, and one lab that is gone
Docker. Start with the official reference pages used above, then build something non-trivial.
- Dockerfile reference ↗Every instruction, with its exact behavior. Look up
COPY --from,USERandENVhere.docs.docker.com - Build cache invalidation ↗The rules for when a layer is reused, with the cases that surprise people.docs.docker.com
- Multi-stage builds ↗Docker's guide to splitting build and runtime stages.docs.docker.com
- Building best practices ↗Base image choice, digest pinning, cache-friendly ordering and cleanup, on one page.docs.docker.com
- Docker Curriculum ↗A hands-on tutorial that packages a Flask app, adds Elasticsearch with Compose and deploys to AWS.docker-curriculum.com
Kubernetes. The official tutorials are free, and the Hello Minikube tutorial in the same documentation section works against a local cluster.
- Learn Kubernetes Basics ↗The official walk-through: create a cluster, deploy an app, explore it, expose it and scale it.kubernetes.io
- Kubernetes overview ↗What Kubernetes is and what it is for, in the project's own words.kubernetes.io
- Kubernetes The Hard Way ↗Kelsey Hightower's guide to bootstrapping a cluster by hand. Slow, and the best way to learn what the pieces are.github.com
06 · PRACTICE
Time your own rebuild
docker build --progress=plain .. Change one source file and build again; the install steps should say CACHED. Now move COPY . . above the install, change a file, and rebuild. Watch the install run again. Finish with docker history on the image and find the largest layer.07 · FAQ
Frequently asked questions
Why does my Docker build reinstall dependencies every time?
Because a layer before the install changed. If you copy the whole project before running the install, any source edit invalidates that layer and everything after it. Copy only the package manifest and lockfile first, run the install, and copy the rest of the source afterward.
How do I make a Docker image smaller?
Use a multi-stage build so compilers and dev dependencies stay in the build stage, start from a slim or minimal base image, install production dependencies only, and keep a dockerignore file so unneeded files never enter the build context. Run docker history to find the largest layers.
Should I learn Docker or Kubernetes first?
Docker first. Kubernetes schedules containers across machines, so it assumes you already understand images, containers, ports and volumes. Learn to write and debug a Dockerfile, then move to the official Kubernetes basics tutorial.
Is Docker Desktop free?
For personal use, education, non-commercial open source and small businesses with fewer than 250 employees and less than $10 million in annual revenue, yes. Larger organizations need a paid subscription for professional use, according to Docker's license terms.
Do I need Kubernetes for a small project?
Usually not. One server running Docker with Compose handles a small app well. Kubernetes pays off when you run many services across several machines and need automated rollouts and self-healing.