You've mastered Dockerfiles, networks, volumes, and registries β but what happens when you need to run many containers across many machines? That's where container orchestration comes in. This post walks you through the basics of orchestration using Docker Swarm and Kubernetes, including real deployment files, commands, and best practices.
Container orchestration automates the deployment, management, scaling, and networking of containers. It's the brain that keeps everything alive, replicated, load balanced, and up to date across machines.
Docker Swarm is built directly into Docker Engine β it's simpler than Kubernetes and great for small-to-medium deployments.
docker swarm init
It will output a token to join more nodes:
docker swarm join --token SWMTKN-1-xxx IP:2377
docker service create \
--name web \
--replicas 3 \
-p 80:80 \
nginx
docker service scale web=5
Swarm supports Compose files out of the box:
version: '3.8'
services:
app:
image: aelify/myapp:latest
deploy:
replicas: 3
restart_policy:
condition: on-failure
ports:
- "3000:3000"
redis:
image: redis:alpine
docker stack deploy -c docker-compose.yml mystack
Kubernetes (K8s) is the most powerful and widely adopted orchestrator. It's more complex but ideal for enterprise workloads.
apiVersion: apps/v1
kind: Deployment
metadata:
name: node-deployment
spec:
replicas: 3
selector:
matchLabels:
app: node-app
template:
metadata:
labels:
app: node-app
spec:
containers:
- name: node
image: aelify/node-app:latest
ports:
- containerPort: 3000
apiVersion: v1
kind: Service
metadata:
name: node-service
spec:
type: LoadBalancer
selector:
app: node-app
ports:
- protocol: TCP
port: 80
targetPort: 3000
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl get pods
kubectl get services
kubectl scale deployment node-deployment --replicas=5
Simply reapply the updated deployment.yaml β Kubernetes handles rolling restarts without downtime.
| Feature | Docker Swarm | Kubernetes |
|---|---|---|
| Ease of Use | β Easy | π§ Steep learning |
| Built-in with Docker | β Yes | β Needs separate install |
| Community & Ecosystem | π‘ Medium | π Huge |
| Advanced Features | π« Limited | π Full control |
Whether you're launching on a Raspberry Pi or building out microservices at scale β understanding Docker Swarm and Kubernetes is essential to your DevOps journey. Start small, experiment, and watch your containers orchestrate themselves like magic π§ββοΈ.
In our next post, we'll dive into π (Post 6): CI/CD with Docker β Automating Builds, Tests & Deployment!
β Blog by Aelify (ML2AI.com)
π Documentation Index