DockerNode.jsDevOpsCI/CD
Development vs Production Containers
A container that works on your laptop is not necessarily production-ready. Production images should be small, secure, and observable.
Multi-Stage Builds
# Build stage
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
# Production stage
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app .
RUN npm prune --production
EXPOSE 3000
CMD ["node", "server.js"]
The build stage installs all dependencies (including dev). The production stage only keeps what is needed to run.
Health Checks
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
CMD wget -qO- http://localhost:3000/health || exit 1
Orchestrators (Docker Compose, Kubernetes) use health checks to restart unhealthy containers automatically.
Security Hardening
- Run as a non-root user.
- Use
.dockerignoreto exclude.env,.git, andnode_modules. - Pin base image versions to avoid surprise upgrades.
- Scan images with tools like Trivy or Snyk.
Orchestration
For small deployments, Docker Compose is sufficient. For scale, Kubernetes with Helm charts provides declarative infrastructure. Either way, treat your container definitions as code: version them, review them, test them.