By Kevin McAleer, 5 Minutes
Page last updated November 24, 2026

By the end of this lesson you will be able to write a stack file with the deploy: settings that tell Swarm how many copies to run, how to update them and where to put them.
A stack file has the same top-level sections as any Compose file:
services: the containers you want, each with an image:, ports: and so onvolumes: named storagenetworks: extra networks (Swarm creates a default one for the stack)configs: and secrets: small files Swarm hands to containersWhat makes it a swarm file is the deploy: key inside each service. Docker Compose on a single machine ignores most of it. docker stack deploy reads all of it.
Save this as stack.yml on a manager. Both images are official and are published for arm64:
services:
web:
image: nginx:alpine
ports:
- "8180:80"
healthcheck:
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1/"]
interval: 10s
timeout: 3s
retries: 3
deploy:
replicas: 3
update_config:
parallelism: 1
delay: 10s
order: start-first
failure_action: rollback
restart_policy:
condition: on-failure
resources:
limits:
memory: 64M
cache:
image: redis:7-alpine
volumes:
- cache-data:/data
deploy:
replicas: 1
placement:
constraints:
- node.role == manager
volumes:
cache-data:
Let’s walk through it.
image and portsweb runs the small nginx:alpine image. "8180:80" publishes container port 80 on port 8180 of every node, thanks to the routing mesh from lesson 5.
healthcheckSwarm only sends traffic to, and counts as ready, a task whose health check passes. Here every ten seconds we fetch the home page from inside the container. We use 127.0.0.1 rather than localhost to avoid IPv6 surprises.
deploy: replicasThree copies of web. If one dies, Swarm starts another to get back to three.
deploy: update_configHow a rolling update behaves. parallelism: 1 changes one task at a time, delay: 10s waits between them, order: start-first starts the new task before stopping the old one so nothing goes dark, and failure_action: rollback automatically goes back if the new version fails.
deploy: restart_policy and resourceson-failure restarts crashed tasks. limits: memory: 64M stops one greedy container from starving the whole Pi.
deploy: placementConstraints pin a service to certain nodes. Our cache uses a named volume, and a named volume lives on one node’s disk only. If Swarm moved the cache to another node, it would start with an empty volume. Pinning it to the manager keeps the data where it is. This is the most common surprise for newcomers, and we revisit it in the backup lesson.
Swarm will not interpret every key as you expect, so preview it:
docker stack config -c stack.yml
This prints the file as Docker understands it, and complains about syntax errors. Indentation in YAML matters: use spaces, never tabs.
Notice we put no password anywhere. For a real database you would store it as a Swarm secret, created with docker secret create, which containers read from a file under /run/secrets/. It is far safer than an environment: line committed to Git. We don’t use one in this stack, but the Try it Yourself below has you explore it.
replicas: 3 to replicas: 4 and "8180:80" to "8181:80", then run docker stack config -c stack.yml and spot the change.cpus: "0.50" under resources: limits: for web and check the file still validates.docker secret create and sketch how you would give a postgres service its password without writing it in stack.yml.yaml: line 12: found character that cannot start any token or similar.Why: YAML uses indentation for structure and forbids tabs.
placement constraint, or use shared storage such as NFS.Why: A named volume is stored on a single node, so a task placed elsewhere starts with a fresh empty volume.
docker exec <container> wget -q -O /dev/null http://127.0.0.1/.Quick quiz 1 of 3
What does order: start-first do during an update?
start-first means there is always a task running during the update, so there is no gap in service.Quick quiz 2 of 3
Why did we pin cache to the manager?
Quick quiz 3 of 3
Which command previews how Docker reads your stack file?
docker stack config prints the processed file and reports errors before you deploy.
Comments