Skip to content

Deploying NestJS with Docker (Because Containers Are Like Bento Boxes) ​

So you’ve survived Vercel, AWS, and DigitalOcean. Now you want Docker.

Why? Because containers are like those magical lunch boxes πŸ₯‘:

  • Everything you need goes in
  • It works the same everywhere
  • You can ship it, scale it, and brag about it

🧐 Why Docker? ​

  • Consistency: Works locally, works in the cloud.
  • Isolation: No random dependencies stealing your lunch.
  • Scalability: Spin up multiple copies like minions.
  • Cool points: You can say β€œI run microservices in containers” and people nod.

πŸ—‚οΈ Folder Structure ​

Here’s what your repo might look like:

nest-docker/
│── src/
β”‚   └── main.ts
│── dist/
β”‚   └── main.js
│── package.json
│── Dockerfile
│── .dockerignore
│── tsconfig.json

🐳 Step 1: Create .dockerignore ​

node_modules
dist
npm-debug.log

Keeps the image light. Nobody likes heavy containers.

🐳 Step 2: Create Dockerfile ​

dockerfile
# Stage 1: Build
FROM node:20-alpine AS builder

WORKDIR /app

COPY package*.json ./
RUN npm ci

COPY tsconfig*.json ./
COPY src ./src

RUN npm run build

# Stage 2: Production
FROM node:20-alpine

WORKDIR /app

COPY package*.json ./
RUN npm ci --omit=dev

COPY --from=builder /app/dist ./dist

EXPOSE 3000

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

Two-stage build = small, fast, production-ready image πŸš€

🐳 Step 3: Build Docker Image ​

bash
docker build -t nest-docker-app .

🐳 Step 4: Run Locally (Optional, but fun) ​

bash
docker run -p 3000:3000 nest-docker-app

Now open your browser: http://localhost:3000

Your app is alive inside a magical container! 🐳✨

🐳 Step 5: Deploy to Any Cloud ​

5a: DigitalOcean ​

  • Push your image to Docker Hub or DigitalOcean Container Registry
  • In App Platform, select Dockerfile deployment
  • DO builds and runs your container magically

5b: AWS ​

  • Push image to ECR
  • Use Elastic Beanstalk Docker platform or ECS / Fargate
  • AWS scales your container like a pro

5c: Any cloud (Heroku, Railway, Render) ​

  • Most modern PaaS accept Dockerfile
  • Push β†’ Deploy β†’ Sip chai β†’ Celebrate πŸŽ‰

🎯 TL;DR ​

StepCommand / Notes
Build imagedocker build -t nest-docker-app .
Run locallydocker run -p 3000:3000 nest-docker-app
Deploy to cloudPush to Docker Hub / Cloud registry, run / scale

🏁 Summary ​

Docker is like a magic lunchbox: pack it once, eat it anywhere.

  • Works on your laptop βœ…
  • Works on DigitalOcean βœ…
  • Works on AWS βœ…
  • Impresses your dev friends βœ…

Bonus: You can tell people, β€œI containerized my NestJS app and it survived the apocalypse.” 😎

Built by noobs, for noobs, with love πŸ’»β€οΈ