Stack Files & deploy
A stack file looks like a Compose file, and many Compose files deploy as stacks unchanged. But docker stack deploy reads the file with the older Compose file format 3.x schema and runs it on a cluster, so some keys mean something different, some are ignored, and some are rejected outright. This lesson is the map: what the deploy: section controls, which Compose keys don't apply to Swarm, and how to layer several files.
The deploy section
deploy: is where a stack file talks to the orchestrator. docker compose up ignores most of it on a single host; Swarm uses all of it.
# $ docker stack config -c stack.yaml
# --- stack.yaml
services:
api:
image: ghcr.io/acme/shop-api:2.4.1
healthcheck:
test: ["CMD", "wget", "-qO-", "http://localhost:9000/health"]
interval: 10s
timeout: 3s
retries: 3
start_period: 20s
deploy:
mode: replicated
replicas: 4
placement:
constraints: [node.role == worker]
preferences:
- spread: node.labels.zone
max_replicas_per_node: 2
resources:
limits: {cpus: "0.50", memory: 512M}
reservations: {cpus: "0.25", memory: 256M}
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
window: 2m
update_config:
parallelism: 1
delay: 10s
order: start-first
failure_action: rollback
monitor: 30s
rollback_config:
parallelism: 2
order: stop-first
labels:
team: checkoutversion: "3.13"
services:
api:
deploy:
mode: replicated
replicas: 4
labels:
team: checkout
update_config:
parallelism: 1
delay: 10s
failure_action: rollback
monitor: 30s
order: start-first
rollback_config:
parallelism: 2
order: stop-first
resources:
limits:
cpus: "0.50"
memory: "536870912"
reservations:
cpus: "0.25"
memory: "268435456"
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
window: 2m0s
placement:
constraints:
- node.role == worker
preferences:
- spread: node.labels.zone
max_replicas_per_node: 2
healthcheck:
test:
- CMD
- wget
- -qO-
- http://localhost:9000/health
timeout: 3s
interval: 10s
retries: 3
start_period: 20s
image: ghcr.io/acme/shop-api:2.4.1
What changed in the printout: memory sizes became bytes (512M → "536870912"), durations were normalised (2m → 2m0s) and the healthcheck test became a list. Each block has its own lesson: placement, reservations & limits, healthchecks, rolling updates and rollbacks.
Compose keys that Swarm doesn't use
Some keys are valid in the 3.x schema but only make sense on one host. docker stack config prints them without complaint; docker stack deploy warns and skips them:
# $ docker stack config -c stack.yaml
# --- stack.yaml
services:
api:
build: ./api
image: ghcr.io/acme/shop-api:2.4.1
container_name: shop-api
restart: always
depends_on: [db]
db:
image: postgres:17version: "3.13"
services:
api:
build:
context: ./api
container_name: shop-api
depends_on:
- db
image: ghcr.io/acme/shop-api:2.4.1
restart: always
db:
image: postgres:17
$ docker stack deploy -c stack.yaml shop
Ignoring unsupported options: build, restart
Ignoring deprecated options:
container_name: Setting the container name is not supported.
Creating network shop_default
Creating service shop_api
Creating service shop_db(sample output; the exact list depends on the CLI version.) depends_on is dropped without any message, so don't rely on the warnings to find every single-host assumption.
| Key | On a single host (Compose) | In a stack |
|---|---|---|
build |
builds the image | ignored: build and push in CI, reference by image: |
container_name |
fixed container name | ignored: tasks are named <stack>_<svc>.<slot>.<id> |
restart |
restart policy | ignored: use deploy.restart_policy |
depends_on |
start order | ignored: services start in any order, so apps must retry connections |
links, network_mode, privileged, devices, ipc, pid, security_opt, shm_size … |
single-host features | listed as "unsupported" and skipped; use overlay networks, or docker service create flags where one exists |
Newer Compose Specification keys don't exist in the 3.x schema at all, and the stack loader rejects them with a schema error:
# $ docker stack config -c stack.yaml
# --- stack.yaml
services:
api:
image: ghcr.io/acme/shop-api:2.4.1
profiles: [debug]services.api: additional property 'profiles' is not allowed
# $ docker stack config -c stack.yaml
# --- stack.yaml
name: shop
services:
api:
image: ghcr.io/acme/shop-api:2.4.1(root): additional property 'name' is not allowed
| Rejected by stacks | Why it exists in Compose | Stack alternative |
|---|---|---|
top-level name: |
project name | the stack name argument of docker stack deploy |
profiles |
optional services | separate files layered with -c |
depends_on with condition: |
wait for healthy | apps retry; healthchecks + update policies |
develop / watch |
live reload | none (dev tooling) |
pull_policy |
when to pull | docker stack deploy --resolve-image |
configs content:, secrets environment: |
inline sources | file: or external: true |
Several files: -c base -c override
Like docker compose -f a -f b, you can pass -c several times. Later files override earlier ones key by key: scalars are replaced, maps merged, and some lists (like ports) are appended.
# $ docker stack config -c base.yaml -c prod.yaml
# --- base.yaml
services:
api:
image: ghcr.io/acme/shop-api:2.4.1
environment:
LOG_LEVEL: info
FEATURE_X: "off"
ports: ["8080:9000"]
deploy:
replicas: 1
labels: [team=checkout]
# --- prod.yaml
services:
api:
environment:
LOG_LEVEL: warn
ports: ["9443:9000"]
deploy:
replicas: 6
labels: [tier=prod]
resources:
limits: {memory: 1G}version: "3.13"
services:
api:
deploy:
replicas: 6
labels:
team: checkout
tier: prod
resources:
limits:
memory: "1073741824"
environment:
FEATURE_X: "off"
LOG_LEVEL: warn
image: ghcr.io/acme/shop-api:2.4.1
ports:
- mode: ingress
target: 9000
published: 8080
protocol: tcp
- mode: ingress
target: 9000
published: 9443
protocol: tcp
replicas and LOG_LEVEL were overridden, FEATURE_X and the team label kept, the tier label and the memory limit added, and the service now publishes both ports. If you wanted to replace the port, there's no way to do it from the override file: Compose's !override and !reset YAML tags are not understood by the stack loader. Keep lists such as ports out of the base file and set them in each environment file instead.
Deploying the merged result:
$ docker stack deploy -c base.yaml -c prod.yaml shopUseful deploy flags
| Flag | Effect |
|---|---|
--with-registry-auth |
send your registry credentials to the nodes so they can pull private images |
--prune |
remove services that are no longer in the file |
--resolve-image always|changed|never |
whether to ask the registry for the image digest (pins tasks to the exact image) |
--detach=false |
wait until the services have converged and show progress |
A tip on --with-registry-auth: without it, nodes that never logged in fail to pull private images, and tasks sit in Rejected with "No such image" or "pull access denied" errors (see Troubleshooting).
Note: The Compose to Swarm lesson walks the other direction: making a Compose project stack-ready.
Learn more: Stacks · Stack Variables & docker stack config · Updating Stacks
Test yourself
What happens to depends_on: [db] when the file is deployed with docker stack deploy?
- Swarm starts db first and waits for it
- It is ignored: services start in any order
- The deploy fails
- Swarm waits for db to be healthy
base.yaml publishes 8080:9000 and prod.yaml publishes 9443:9000 for the same service. What does docker stack config -c base.yaml -c prod.yaml show?
- Only 9443
- Only 8080
- Both ports
- An error about duplicate targets
Which of these keys makes the stack loader fail with a schema error?
deploy.replicashealthcheckprofilesenvironment