Lab 13: Containerizing Node.js
Overview
Step 1: Basic Dockerfile (and Why It's Bad)
# BAD: Don't do this
FROM node:20
WORKDIR /app
COPY . .
RUN npm install
EXPOSE 3000
CMD ["node", "server.js"]
# Problems:
# 1. Uses full node image (1GB+) instead of alpine (~150MB)
# 2. COPY . . before npm install breaks layer caching
# 3. Runs as root — security risk
# 4. Dev dependencies included in production
# 5. No health check
# 6. No graceful shutdown handlingStep 2: Production Dockerfile
Step 3: .dockerignore
Step 4: Health Check Script
Step 5: Graceful Shutdown in Containerized Apps
Step 6: Docker Compose for Full Stack
Step 7: Layer Caching Optimization
Step 8: Capstone — Build & Run Demo
Summary
Best Practice
Implementation
Benefit
Last updated
