Most developers write a Dockerfile once, get it working, and never look at it again.
The result is usually an image that:
- Ships build tools nobody needs in production
- Includes devDependencies and source code that should never be there
- Takes forever to rebuild after a one-line change
I recently built a Dockerfile for a full-stack monorepo (React client + Node server) that fixes all three. It's only about 40 lines, and every one of them has a reason.
Let me walk you through it.
The Big Idea: Don't Ship Your Kitchen
Think of a restaurant. The kitchen has knives, pans, and raw ingredients. The customer gets a finished plate.
A multi-stage build works the same way:
🔨 Stage 1: build
Compiles the frontend → ❌ Not shipped
📦 Stage 2: production-deps
Installs runtime packages only → ✅ Only node_modules shipped
🚀 Stage 3: final
Assembles the finished image → ✅ Shipped
Only the last stage becomes your image. Everything else is thrown away.
Stage 1: Build (The Messy Kitchen)
FROM node:20-alpine AS build WORKDIR /app COPY package.json package-lock.json ./ COPY apps/server/package.json apps/server/package.json COPY apps/client/package.json apps/client/package.json RUN npm ci COPY apps ./apps RUN npm run build -w client Three things are happening here:
Alpine, not full Node. node:20-alpine is built on a tiny Linux distro. Smaller base means smaller image.
Copy package.json BEFORE your code. This is the single biggest speed trick in Docker. Docker caches each step. If your dependencies didn't change, it skips npm ci entirely. Edit one React component and your rebuild takes seconds, not minutes.
npm ci, not npm install. It follows your lockfile exactly. Same versions every time, on every machine. No more "works on my laptop."
Stage 2: Production Dependencies (Only What Runs)
FROM node:20-alpine AS production-deps WORKDIR /app COPY package.json package-lock.json ./ COPY apps/server/package.json apps/server/package.json COPY apps/client/package.json apps/client/package.json RUN npm ci --omit=dev One flag does the magic: --omit=dev.
No TypeScript compiler. No Vite. No test runners. No linters. Just the packages your server needs at runtime.
This is often where the biggest size savings come from.
Stage 3: The Final Image (Clean and Lean)
FROM node:20-alpine ENV NODE_ENV=production \ PORT=4000 WORKDIR /app COPY --from=production-deps /app/node_modules ./node_modules # ...package.json files... COPY apps/server ./apps/server COPY --from=build /app/apps/client/dist ./apps/client/dist Look closely at COPY --from=. It pulls files from earlier stages.
From the build stage, we take only apps/client/dist, the compiled frontend. Not the source. Not the build tools. Not the cache.
The final image contains exactly three things:
- Production node_modules
- Server code
- Built frontend files
That's it. Nothing extra.
The Underrated Hero: HEALTHCHECK
Most Dockerfiles skip this. Don't.
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \ CMD ["node", "-e", "fetch('http://127.0.0.1:'+(process.env.PORT||4000)+'/api/health')..."] A running container isn't the same as a working app. Your process can be alive while your API is completely stuck.
This tells Docker: "Every 30 seconds, hit /api/health. If it fails 3 times in a row, mark this container unhealthy."
Orchestrators and monitoring tools can then restart it automatically.
The clever part: it uses Node's built-in fetch instead of curl. Alpine doesn't ship with curl, so this avoids installing an extra package just for a health check.
Small Details That Matter
CMD ["npm", "start", "-w", "server"] uses exec form (the array syntax). That means Docker signals like SIGTERM reach your app properly, so you get graceful shutdowns instead of abrupt kills.
EXPOSE 4000 is documentation only. It doesn't publish the port. You still need -p 4000:4000 when running.
NODE_ENV=production switches many libraries (Express included) into optimized mode.
The Cheat Sheet
✅ Multi-stage build = smaller, safer image
✅ Copy package.json first = faster rebuilds
✅ npm ci = reproducible installs
✅ --omit=dev = no dev tools in production
✅ COPY --from= = take only what you need
✅ HEALTHCHECK = know when your app is actually broken
✅ Exec-form CMD = clean shutdowns
One More Thing (Don't Skip This)
Pair this Dockerfile with a .dockerignore:
node_modules .git **/dist .env Without it, you risk sending your local node_modules and even your .env secrets into the build context. That's a mistake you only make once.
Final Thought
A good Dockerfile isn't about being clever. It's about being intentional: every line either makes your image smaller, your builds faster, or your deployments safer.
Next time you open your Dockerfile, ask of each line: "Does this need to exist in production?"
If the answer is no, it belongs in a build stage, not your final image.
What's the biggest Docker image you've ever shipped? 👇 Drop your number in the comments. I'm curious how bad it gets.
♻️ Repost if this helps someone on your team. ➕ Follow for more practical full-stack and DevOps breakdowns.