Docker for Developers: Containerizing Your Node.js App
Docker packages your application and its dependencies into a portable image. For Node.js APIs and React frontends, containers ensure dev, CI, and production environments behave the same way.
A Production-Ready Dockerfile
Multi-stage builds keep images small:
FROM node:20-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:20-alpine
WORKDIR /app
COPY --from=build /app/dist ./dist
COPY --from=build /app/node_modules ./node_modules
ENV NODE_ENV=production
EXPOSE 3000
CMD ["node", "dist/server.js"]
docker-compose for Local Dev
Pair your API with PostgreSQL or Redis:
services:
api:
build: .
ports: ["3000:3000"]
environment:
DATABASE_URL: postgres://user:pass@db:5432/app
db:
image: postgres:15
environment:
POSTGRES_PASSWORD: pass
Best Practices
Use .dockerignore to exclude node_modules and secrets. Pin base image versions. Scan images in CI for vulnerabilities.
Conclusion
Docker is table stakes for modern full stack deployment. Master a solid Dockerfile and compose setup before moving to Kubernetes or cloud-specific runtimes.