Back
2/4

Services & Scaling

+20 XP on completion

#Services & Scaling

After this lesson you'll know:

  • what a Swarm Service is
  • how to scale services
  • what rolling updates are

#From Containers to Services

A container runs on one host. A service runs across the whole cluster — Docker automatically distributes tasks across available nodes.

# Create a service (3 replicas)
docker service create --name web --replicas 3 -p 80:80 nginx

docker service ls         # All services
docker service ps web     # Where are the tasks running?

#Scaling — the game changer

# Scale up
docker service scale web=10

# Scale down
docker service scale web=2

# In one command
for i in 3 5 10 2 7; do
  docker service scale web=$i
  sleep 2
done

Each scaling command takes seconds. No new server, no new deployment.

#Rolling Updates

# Deploy a new image — without downtime
docker service update --image nginx:1.25 --update-parallelism 2 --update-delay 10s web

# This means:
# 2 containers are updated simultaneously
# 10 second pause between batches
# On failure, automatically rolls back
# Track update progress
docker service inspect web --pretty

docker service ps web  # Can you see the rolling progress?

#Service Configuration in Detail

# With limits and restart policy
docker service create   --name api   --replicas 3   --limit-cpu 0.5   --limit-memory 256M   --restart-condition any   --update-parallelism 1   --update-delay 5s   --rollback-parallelism 0   --rollback-monitor 20s   my-api:latest

#✋ Try it out

  • docker service create --name whoami --replicas 3 -p 8080:80 containous/whoami — create a service. Visit http://localhost:8080 multiple times — do you see different container IDs?
  • Scale the service to 10 with docker service scale whoami=10. Check with docker service ps whoami — how many tasks do you see?
  • Perform a rolling update: docker service update --image nginx:alpine whoami — what happens?

#📌 Summary

  • A service distributes container tasks across the cluster
  • docker service scale changes replica count in seconds
  • Rolling updates update containers without downtime
← → to navigate