Docker Compose to Kubernetes Migration
Kompose conversion with manual refinements.
Why Migrate from Docker Compose to Kubernetes?
Docker Compose is excellent for local development and small deployments, but as your application grows, you need features that Compose can't provide: auto-scaling, self-healing, rolling updates, and multi-node scheduling. Kubernetes delivers all of these, and the migration path is well-defined.
Kompose: Automated Conversion
Kompose is the official tool for converting Docker Compose files to Kubernetes manifests:
# Install Kompose
curl -L https://github.com/kubernetes/kompose/releases/download/v1.31.2/kompose-linux-amd64 -o kompose
chmod +x kompose && sudo mv kompose /usr/local/bin/
# Convert docker-compose.yml
kompose convert -f docker-compose.ymlThis generates Deployment and Service YAML files for each service defined in your Compose file.
What Kompose Handles Well
- Service definitions → Deployments + Services
- Port mappings → Service port configurations
- Environment variables → ConfigMaps or inline env vars
- Named volumes → PersistentVolumeClaims
- Restart policies → Pod restart policies
What Requires Manual Refinement
- Health checks — Compose healthcheck translates to liveness probes, but you should add readiness probes manually
- Resource limits — Compose
mem_limitconverts, but CPU limits need tuning - Secrets management — Move from .env files to Kubernetes Secrets
- Networking — Compose networks don't map directly; use Kubernetes Services for inter-service communication
- Init containers — Compose
depends_onneeds conversion to init containers for startup ordering
Step-by-Step Migration
- Audit your docker-compose.yml — List all services, volumes, networks, and dependencies
- Run Kompose convert — Generate initial Kubernetes manifests
- Add health probes — Define liveness and readiness probes for each service
- Configure resource requests/limits — Size based on actual usage from Docker stats
- Externalize configuration — Move environment variables to ConfigMaps and secrets to Kubernetes Secrets
- Set up persistent storage — Create StorageClasses and PVCs for stateful services
- Configure Ingress — Replace Docker port mappings with Ingress resources
- Test in a staging cluster — Validate all services communicate correctly
- Set up CI/CD — Automate image builds and manifest deployments
- Migrate production — Use a blue-green strategy for zero-downtime cutover
Example: Before and After
Docker Compose:
services:
web:
image: myapp:latest
ports:
- "8080:80"
environment:
- DB_HOST=postgres
depends_on:
- postgres
postgres:
image: postgres:15
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:Kubernetes equivalent:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: myapp:latest
ports:
- containerPort: 80
env:
- name: DB_HOST
value: postgres
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 256Mi
livenessProbe:
httpGet:
path: /health
port: 80
initialDelaySeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 80
initialDelaySeconds: 5Eazy SaaS Tip: We recommend running your application in both Docker Compose (for development) and Kubernetes (for production) simultaneously. Use Skaffold or Tilt to maintain a smooth developer experience while deploying to K8s in CI/CD.