Why Containers?
Containers solve the classic "it works on my machine" problem. By packaging your application with all its dependencies into a portable unit, you get consistent behavior from development to production.
Docker Fundamentals
Writing a Good Dockerfile
The order of instructions matters — Docker caches each layer. Put things that change rarely at the top:
FROM node:20-alpine AS base
# Dependencies rarely change, so cache them
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
# Source code changes often
COPY . .
RUN npm run build
# Production image — minimal size
FROM node:20-alpine AS production
WORKDIR /app
COPY --from=base /app/dist ./dist
COPY --from=base /app/node_modules ./node_modules
EXPOSE 3000
CMD ["node", "dist/index.js"]Kubernetes: Orchestration at Scale
When you have multiple containers that need to communicate, scale, and recover from failures, you need an orchestrator. Kubernetes (K8s) is the industry standard.
Core Concepts
- Pod: The smallest deployable unit — one or more containers sharing a network namespace
- Deployment: Manages replicas of a Pod, handles rolling updates
- Service: Stable network endpoint for a set of Pods
- Ingress: Routes external HTTP traffic to Services
A Simple Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
spec:
containers:
- name: my-app
image: my-app:1.0.0
ports:
- containerPort: 3000
resources:
requests:
memory: "128Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "500m"Health Checks
Always configure liveness and readiness probes. Without them, Kubernetes can't tell if your app is actually working:
livenessProbe:
httpGet:
path: /health
port: 3000
initialDelaySeconds: 30
periodSeconds: 10Conclusion
Docker and Kubernetes form the backbone of modern application deployment. The learning curve is real, but the operational benefits — reliability, scalability, and consistency — are worth it.
Comments (0)
Sign in to join the conversation.
No comments yet. Be the first to share your thoughts.