Orchestration Overview
Docker and Compose run containers on one machine. Production systems often need more: several servers, automatic recovery when a server dies, rolling updates without downtime, and scaling with load. That is the job of a container orchestrator. This lesson is a conceptual tour; each orchestrator deserves its own course (see the Kubernetes course for the full story).
What an orchestrator does
| Task | Example |
|---|---|
| Scheduling | place 6 replicas of api on the nodes that have enough CPU and memory |
| Self-healing | a node dies; its containers are recreated elsewhere |
| Scaling | go from 6 to 12 replicas, manually or based on CPU load |
| Service discovery & load balancing | one stable name/IP for all api replicas |
| Rolling updates & rollback | replace replicas a few at a time; undo if health checks fail |
| Config & secrets | distribute configuration and secrets to the right containers |
All of them follow the same core idea: you declare the desired state ("6 replicas of shop-api:2.4.1"), and a control loop keeps comparing it with the actual state and acting on the difference. This is called reconciliation.
desired = {"api": 4, "worker": 2}
actual = {"api": ["api-1", "api-2"], "worker": ["worker-1", "worker-2", "worker-3"]}
counter = {"api": 2, "worker": 3}
def reconcile():
actions = []
for service, want in desired.items():
have = actual.setdefault(service, [])
while len(have) < want:
counter[service] = counter.get(service, 0) + 1
name = f"{service}-{counter[service]}"
have.append(name)
actions.append(f"start {name}")
while len(have) > want:
actions.append(f"stop {have.pop()}")
return actions or ["nothing to do"]
print("loop 1:", reconcile())
print("loop 2:", reconcile())
actual["api"].remove("api-2") # a container crashed
print("crash! loop 3:", reconcile())
desired["worker"] = 4 # someone scaled up
print("scaled, loop 4:", reconcile())
print("state:", actual)loop 1: ['start api-3', 'start api-4', 'stop worker-3']
loop 2: ['nothing to do']
crash! loop 3: ['start api-5']
scaled, loop 4: ['start worker-4', 'start worker-5']
state: {'api': ['api-1', 'api-3', 'api-4', 'api-5'], 'worker': ['worker-1', 'worker-2', 'worker-4', 'worker-5']}
The loop doesn't care why reality differs from the goal (a crash, a scale command, a new deployment): it just closes the gap. That makes the system self-healing.
Scheduling
When a new replica must start, the scheduler picks a node. It filters out nodes without enough free resources (based on the replicas' requests/reservations), then scores the rest, for example spreading replicas of the same service across nodes so one failure doesn't take them all.
nodes = {"node-a": {"cpu": 4.0, "mem": 8192, "pods": []},
"node-b": {"cpu": 2.0, "mem": 4096, "pods": []},
"node-c": {"cpu": 4.0, "mem": 4096, "pods": []}}
def schedule(name, service, cpu, mem):
fits = [n for n, s in nodes.items() if s["cpu"] >= cpu and s["mem"] >= mem]
if not fits:
print(f"{name:<9} Pending: no node has {cpu} CPU and {mem} MiB free")
return
# prefer the node running the fewest replicas of this service, then the most free memory
best = min(fits, key=lambda n: (sum(p.startswith(service) for p in nodes[n]["pods"]), -nodes[n]["mem"], n))
nodes[best]["cpu"] -= cpu
nodes[best]["mem"] -= mem
nodes[best]["pods"].append(name)
print(f"{name:<9} -> {best}")
for i in range(1, 5):
schedule(f"api-{i}", "api", 1.0, 1024)
schedule("db-1", "db", 2.0, 4096)
schedule("ml-1", "ml", 3.0, 6144)
for n, s in nodes.items():
print(f"{n}: {s['pods']} free {s['cpu']} CPU / {s['mem']} MiB")api-1 -> node-a
api-2 -> node-b
api-3 -> node-c
api-4 -> node-a
db-1 -> node-a
ml-1 Pending: no node has 3.0 CPU and 6144 MiB free
node-a: ['api-1', 'api-4', 'db-1'] free 0.0 CPU / 2048 MiB
node-b: ['api-2'] free 1.0 CPU / 3072 MiB
node-c: ['api-3'] free 3.0 CPU / 3072 MiB
The last replica stays Pending: there is enough free memory and CPU in the cluster in total, but no single node has both. Requests are per node.
The main options
| Orchestrator | In one sentence | When it fits |
|---|---|---|
| Docker Swarm | built into Docker; docker swarm init, then deploy compose files as stacks |
small clusters, teams that know Compose, simplicity first |
| Kubernetes | the industry standard; huge ecosystem, runs everywhere | most production platforms, larger teams, multi-cloud |
| Managed container services (AWS ECS/Fargate, Google Cloud Run, Azure Container Apps) | the cloud runs the orchestrator for you | you want containers without operating a cluster |
| HashiCorp Nomad | a simple, general scheduler (containers and other workloads) | mixed workloads, HashiCorp stack |
From Compose to Swarm
Swarm reads the same compose format, using the deploy: section:
services:
api:
image: ghcr.io/acme/shop-api:2.4.1
deploy:
replicas: 4
update_config:
parallelism: 1
delay: 10s
failure_action: rollback
resources:
limits: {cpus: "1", memory: 512M}
restart_policy:
condition: on-failuredocker swarm init
docker stack deploy -c compose.yaml shop
docker service ls
docker service update --image ghcr.io/acme/shop-api:2.5.0 shop_apiKubernetes in one picture
The same app in Kubernetes is a Deployment (desired replicas of a Pod template) plus a Service (stable name and load balancing):
apiVersion: apps/v1
kind: Deployment
metadata: {name: shop-api}
spec:
replicas: 4
selector: {matchLabels: {app: shop-api}}
template:
metadata: {labels: {app: shop-api}}
spec:
containers:
- name: api
image: ghcr.io/acme/shop-api:2.4.1
ports: [{containerPort: 8000}]
resources:
requests: {cpu: 250m, memory: 256Mi}
limits: {memory: 512Mi}
---
apiVersion: v1
kind: Service
metadata: {name: shop-api}
spec:
selector: {app: shop-api}
ports: [{port: 80, targetPort: 8000}]Your Docker knowledge carries straight over: the image you built, its exposed port, environment variables, volumes, health checks (as probes), resource limits. The orchestrator adds the cluster-wide layer on top.
Tip: you don't need an orchestrator to start. One server with Compose,
restart: unless-stopped, backups and monitoring carries a surprising amount of traffic. Move to an orchestrator when you need high availability across machines or many teams deploying independently.
Go deeper: the Docker Swarm course (with Swarm vs Kubernetes) and the Kubernetes course.
Test yourself
What is reconciliation in an orchestrator?
- Merging Git branches
- A control loop that keeps changing the actual state until it matches the declared desired state
- Compressing images
- Backing up volumes
A cluster has 3 GB free memory in total, spread as 1 GB on each of 3 nodes. A replica requests 2 GB. What happens?
- It runs across two nodes
- It stays Pending, because no single node can fit it
- It runs with 1 GB
- The cluster adds memory automatically
Which orchestrator can deploy a compose file (with a deploy: section) almost unchanged?
- Kubernetes
- Docker Swarm
- Nomad
- Cloud Run