Docker Multi-Stage Builds and Automated CI/CD for Cloud Run Microservices
In a fast-moving software cooperative, deployment pipelines must be reliable, reproducible, and cost-effective. Shipping large container images containing build dependencies, development packages, and source maps slows down deployments and increases cold-start latency.
At EquiSaaS BD, all production deployments cataloged in our public Proof of Work Ledger use lean multi-stage Docker containers deployed across serverless infrastructure.
Here is our production build setup.
1. Multi-Stage Dockerfile for Next.js and Node.js
By separating dependency resolution, compilation, and production runtime into distinct stages, we reduce our final image sizes from over 1.2 GB down to under 120 MB:
# Stage 1: Dependency resolution
FROM node:20-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
# Stage 2: Compilation
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
ENV NODE_ENV=production
RUN npm run build
# Stage 3: Minimal runtime runner
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
RUN addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 nextjs
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
ENV PORT=3000
CMD ["node", "server.js"]
2. Automated Pipeline and Cache Invalidation
Our automated release workflow automatically:
- Executes unit and lint test suites.
- Builds the multi-stage container and tags it with git commit SHA.
- Deploys the service revision to Google Cloud Run.
- Executes global cache purges across Cloudflare edge nodes so end users receive updated assets instantly.
To explore how our team organizes DevOps workflows and cross-functional teams, visit the Departments Explorer or review our verified team contributions at EquiSaaS BD.
Top comments (0)