Docker Swarm部署报错:restart-policy属性不被允许的问题排查
The Issue
You’re hitting a syntax mismatch in your Docker Compose v3 file for Swarm mode deployment. The error pops up because you used the hyphenated restart-policy key under the deploy section—this doesn’t align with the required naming rules for Swarm-specific configuration fields.
The Fix
Update your docker-compose.yml to use the underscore-separated restart_policy key instead:
version: '3' services: postgres: image: fleetit-postgres deploy: restart_policy: # Correct key name (underscore, not hyphen) condition: on-failure ports: - "5432:5432" environment: POSTGRES_USER: "root" POSTGRES_PASSWORD: "demo1234" POSTGRES_DB: "fleetit"
Why This Works
For Docker Compose v3 (used for Swarm stack deployments), all sub-fields under the deploy section follow underscore-based naming conventions. This includes restart_policy, replicas, resources, and more. Your Docker version (18.04.0-ce) fully supports this specification.
As a quick side note: For non-Swarm deployments (using docker-compose up), you’d use the simpler restart: on-failure syntax directly under the service definition. But Swarm requires the deploy.restart_policy structure with the properly formatted key name.
Once you’ve updated the file, re-run your deployment command:
docker stack deploy -c docker-compose.yml stackdemo
内容的提问来源于stack exchange,提问作者fabiomaia

