DockerAdvanced
NestJS অ্যাপ Docker-এ
NestJS মাল্টি-stage docker build, production port ও PostgreSQL/Redis compose — বাংলায়।
10টি অংশবাংলা ভাষায়
NestJS TypeScript-ভিত্তিক — তাই build করে dist produce করতে হয় এবং production image-এ শুধু সেই output থাকে।
package.json scripts
scriptsjson
json
{
"scripts": {
"build": "nest build",
"start": "node dist/main.js",
"start:dev": "nest start --watch",
"start:prod": "node dist/main.js"
}
}Multi-stage Dockerfile
Dockerfiledockerfile
dockerfile
# Stage 1: install + build
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Stage 2: runtime
FROM node:20-alpine
WORKDIR /app
ENV NODE_ENV=production
# শুধু compiled output + production dependencies
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/dist ./dist
EXPOSE 3000
USER node
CMD ["node", "dist/main.js"]PostgreSQL + Redis compose
compose.ymlyaml
yaml
services:
api:
build: .
ports:
- "3000:3000"
environment:
- DATABASE_URL=postgresql://postgres:pass@postgres:5432/nest
- REDIS_HOST=redis
- REDIS_PORT=6379
depends_on:
- postgres
- redis
postgres:
image: postgres:16-alpine
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=pass
- POSTGRES_DB=nest
volumes:
- pg-data:/var/lib/postgresql/data
redis:
image: redis:7-alpine
volumes:
pg-data:Run
Run stepsbash
bash
docker compose up -d --build
docker compose logs -f api
curl http://localhost:3000/api
# migration (যদি থাকে)
docker compose exec api node dist/main.js migration:rundist আর node_modules
Runtime stage-এ devDependencies কপি করবেন না — শুধু production dependencies রাখুন যাতে image হালকা ও নিরাপদ হয়।
শেষ প্যারায় নিজে practice করুন — কোড কপি করে নিজের এডিটরে চালিয়ে দেখুন।