Goal: You form a one-node swarm and run a replicated service. Time estimate: 20 minutes Instructions:
- Initialize Swarm (if not already active).
docker swarm init
Swarm initialized: current node (...) is now a manager - Create a service with three replicas.
docker service create --name swarm-nginx --publish 8088:80 --replicas 3 nginx:1.27-alpine
overall progress: 3 out of 3 tasks - List services and tasks.
docker service ls docker service ps swarm-nginx
REPLICAS 3/3
Expected output: docker service ls shows 3/3 replicas; curl http://localhost:8088 returns nginx HTML (status 200).
Hint: If port 8088 is busy, pick another host port and adjust --publish.
Goal: You scale a service and perform a rolling image update. Time estimate: 25 minutes Instructions:
- Scale the service from Exercise 1 to 5 replicas.
docker service scale swarm-nginx=5
overall progress: 5 out of 5 tasks - Update the image tag.
docker service update --image nginx:1.28-alpine swarm-nginx
overall progress: 5 out of 5 tasks - Confirm tasks use the new image.
docker service ps swarm-nginx --no-trunc | head -n 6
Expected output: Five running tasks; image column shows nginx:1.28-alpine after update completes.
Hint: Watch progress with docker service ps swarm-nginx until all tasks are Running.
Goal: You deploy the module stack file and tear it down cleanly. Time estimate: 25 minutes Instructions:
- From
modules/16-docker-swarm/examples, deploy the stack.docker stack deploy -c swarm-stack.yaml webstack
Creating service webstack_web - Verify three replicas and HTTP response.
docker stack services webstack curl -s -o /dev/null -w "%{http_code}\n" http://localhost:80803/3 200 - Remove the stack.
docker stack rm webstack
Removing service webstack_web - Remove the Exercise 1 service if you no longer need it.
docker service rm swarm-nginx
Expected output: Stack services show 0/0 or disappear after stack rm; port 8080 is free.
Hint: You cannot deploy two stacks that publish the same host port—remove Exercise 1 service or change ports in the YAML.