🚀 Day 25: Docker Multi-Stage Builds – Explained with a TypeScript Node.js App

Ritesh SinghRitesh Singh
2 min read

Welcome to Day 25 of your DevOps journey! Today, we’re mastering Docker Multi-Stage Builds by building a clean and efficient image for a TypeScript-based Node.js app.


🔍 What are Multi-Stage Builds?

Multi-stage builds let you use multiple FROM statements in a Dockerfile, enabling you to separate the build environment from the production environment.

🎯 Benefits:

  • ✅ Smaller image size

  • ✅ Clean, secure production environment

  • ✅ Efficient dependency management


📁 Project Structure

node-ts-app/
├── src/
│   └── index.ts
├── package.json
├── tsconfig.json
├── Dockerfile

🛠 Step-by-Step Breakdown

1️⃣ Create Your Project Directory

mkdir node-ts-app
cd node-ts-app

2️⃣ Create package.json

vim package.json

Paste this content:

{
  "name": "node-ts-app",
  "version": "1.0.0",
  "scripts": {
    "build": "tsc",
    "start": "node dist/index.js"
  },
  "dependencies": {},
  "devDependencies": {
    "typescript": "^5.0.0"
  }
}

3️⃣ Create tsconfig.json

vim tsconfig.json

Paste this content:

{
  "compilerOptions": {
    "target": "ES6",
    "module": "CommonJS",
    "outDir": "dist",
    "strict": true
  },
  "include": ["src/**/*"]
}

4️⃣ Create src/ folder and index.ts

mkdir src
cd src
vim index.ts

Paste this code:

console.log("🚀 Hello from TypeScript inside Docker!");

5️⃣ Go back to root and create Dockerfile

cd ..
vim Dockerfile

Paste this Dockerfile with multi-stage build:

# --------- Stage 1: Build Stage ----------
FROM node:18 AS build

WORKDIR /app

COPY package*.json ./
RUN npm install

COPY . .
RUN npm run build

# --------- Stage 2: Production Stage ----------
FROM node:18-slim

WORKDIR /app

COPY --from=build /app/dist ./dist
COPY package*.json ./

RUN npm install --only=production

CMD ["node", "dist/index.js"]

6️⃣ Build the Docker Image

docker build -t node-ts-multistage .

7️⃣ Run the Docker Container

docker run --rm node-ts-multistage

✅ You should see:

🚀 Hello from TypeScript inside Docker!

📌 Final Outcome

  • 🎯 TypeScript gets compiled in build stage

  • 🎯 Only compiled JS and production deps go in final image

  • 🎯 Clean, minimal, production-ready container


🧠 DevOps Takeaway

You don’t need to write the code—but you should understand how to:

TaskWhy It Matters
Identify the build toolHelps configure Docker builds correctly
Understand the CMDRuns the app inside container
Optimize DockerfilesCreates small, secure images

🔗 Connect with Me

✅ You just mastered Multi-Stage Builds with real-world Docker workflow!

Next up? We’ll deploy this optimized container to the cloud! ☁️

0
Subscribe to my newsletter

Read articles from Ritesh Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ritesh Singh
Ritesh Singh

Hi, I’m Ritesh 👋 I’m on a mission to become a DevOps Engineer — and I’m learning in public every single day.With a full-time commitment of 8–10 hours daily, I’m building skills in: ✅ Linux✅ Git & GitHub✅ Docker & Kubernetes✅ AWS EC2, S3✅ Jenkins, GitHub Actions✅ Terraform, Prometheus, Grafana I post daily blogs on Hashnode, push projects to GitHub, and stay active on LinkedIn and Twitter/X. Let’s connect, collaborate, and grow together 🚀 #100DaysOfDevOps #LearningInPublic #DevOps