Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 38 ยท Projects and Practice

Docker Swarm Practice Questions and Exercises

Test your Swarm skills with scenario questions on secrets, stacks, updates, failures and recovery, hands-on exercises for a lab, and interview-style answers explained.

You have covered secrets and configs, stacks, scaling, updates, operations and a few projects. This final lesson is for practice: scenario questions in the style of real incidents, exercises you can do on a three-node lab, and short model answers. Try each before reading the answer.

Swarm cannot run on this site, so exercises are for your own machine (three small virtual machines or cloud instances work well). The runnable blocks are stand-ins that let you practise reasoning about quorum, schedules and secrets.

Scenario questions

1. A deploy hangs with shop_db at 0/1 and the task is pending. What do you check? Run docker service ps shop_db --no-trunc. A message about constraints means the label node.labels.role == db is not on any node, or the node is drained or down. Fix it with docker node update --label-add role=db node-2 and check docker node ls.

2. After a release, half of the users see errors. How do you stop the damage fastest, and what do you do next? Run docker service rollback shop_api (one level back). Afterwards, find out why automatic protection did not trigger: missing health check, monitor too short, or failure_action: pause. Fix the pipeline, not only the code.

3. Two of your three managers die. Can you deploy? What do you do? No: quorum (2 of 3) is lost, so changes are refused, although running tasks keep serving traffic. On the surviving manager use docker swarm init --force-new-cluster, join two new managers, and verify. Next time spread managers across failure domains and rehearse the procedure.

4. A secret leaked in a log. Steps? Create a new secret with a new name, update services with --secret-rm old --secret-add new, change the credential at its source (the database role, the API provider), remove the old secret, and clean the logs. Rotating only the Swarm object without changing the real credential achieves nothing.

5. You scaled api to 12 but only 8 run. Why might that be? Pending tasks: insufficient reserved CPU or memory, constraints, or max_replicas_per_node. Look at the pending reason in docker service ps api.

6. Why can two replicas of a service end up on the same node although another node is empty? Without a spread preference or max_replicas_per_node, the scheduler balances by task count and available resources, not strictly one per node; new nodes also receive only new tasks, so run docker service update --force after adding capacity.

7. A container is killed with exit code 137. What does it mean? It was killed by SIGKILL: most often the kernel OOM killer after exceeding its memory limit, or Docker after stop_grace_period expired on shutdown. Check docker inspect for OOMKilled and the service's memory limit.

8. Why is docker config wrong for a database password? inspect shows config content and it is not tmpfs-backed. Use a secret.

Hands-on exercises

  1. Initialise a swarm on node 1, join two workers, and promote one worker to manager. Use docker node ls to verify roles.
  2. Create a secret and a service that reads it from /run/secrets. Confirm that a second service without the grant cannot see it.
  3. Write a stack with a proxy and an API; deploy it; scale the API to 6 and back; change an environment variable and watch the rolling update with start-first.
  4. Break a release on purpose (an image whose health check fails). Observe failure_action: rollback.
  5. Drain a worker (docker node update --availability drain node-3) and watch tasks move; re-activate it.
  6. Rotate a config using the versioned-name pattern without any downtime.
  7. Take a backup of /var/lib/docker/swarm on one manager, destroy a lab manager and rebuild with --force-new-cluster.
  8. Add a log-agent as a global service and confirm one task per node.

Stand-in practice: quorum and scheduling

Python Stand-in: will the swarm accept changes?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Stand-in: write the stack, let the checker judge

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Quick revision list

  • Secrets: files in /run/secrets, immutable, rotate with a new name.
  • Stacks: docker stack deploy -c file name, prefixed names, build and depends_on ignored.
  • Updates: parallelism, delay, order, monitor, failure_action; health checks make them safe.
  • Resources: reservations schedule, limits enforce.
  • Operations: back up /var/lib/docker/swarm, keep 3 or 5 managers, rehearse recovery.
  • Choice of tool: Compose, Swarm, Kubernetes or Nomad depends on the team and the workload (Swarm vs Kubernetes).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which quick action reverts a bad release to the previous service spec?

    Rollback returns to the previous spec; deeper versions come from Git.
  2. 2. In a 5-manager swarm, how many failures can you tolerate while still changing state?

    A majority of 3 must stay online, so 2 may fail.
  3. 3. After a credential leak, which step is essential in addition to creating a new Swarm secret?

    The old credential must stop working, otherwise rotation is cosmetic.
  4. 4. A task is pending with 'insufficient resources'. Which fix is most direct?

    The scheduler cannot find a node with enough unreserved CPU or memory.

Summary

  • Practise with incident-style questions: read docker service ps, find the cause, apply the smallest safe fix.
  • Rotation, rollback and quorum recovery are routines to rehearse, not to improvise.
  • A three-node lab is enough to try every exercise in this course.
  • Keep revisiting the track overview and your own notes as the tooling evolves.

Next: Docker Swarm tutorial home