edit-icon download-icon

Service deployment constraints

Last Updated: Feb 12, 2018

Template version

Both Compose V1/V2 and Compose V3 orchestration templates support setting service deployment constraints, and use the same syntax.

Function overview

Set the deployment constraints for a service.

Container Service supports the container deployment constraints compatible with Docker Swarm. You can control the deployment of a container with the Docker Swarm filters.

However, the community version of Docker Compose does not have relevant capabilities to control the direct deployment constraint for a service.

In Container Service, you can add affinity:service in environment to constrain the affinity between services so as to control the service deployment policy. Container Service supports soft affinity and hard affinity between services.

Compose V1/V2

In this example, affinity:service!=master is the deployment constraint for the slave service. In this way, the slave service is always deployed to nodes where the master service is not deployed. Therefore, when a node is invalid, the service availability will not be affected. When your cluster has only one node, as hard anti-affinity is specified, the deployment will fail because it cannot meet the specified mandatory constraints.

Orchestration example:

  1. master:
  2. image: mysql:5.6
  3. environment:
  4. - MYSQL_USER=user
  5. - MYSQL_PASS=test
  6. - REPLICATION_MASTER=true
  7. - REPLICATION_USER=repl
  8. - REPLICATION_PASS=repl
  9. ports:
  10. - 3306
  11. slave:
  12. image: mysql:5.6
  13. environment:
  14. - MYSQL_USER=user
  15. - MYSQL_PASS=test
  16. - REPLICATION_SLAVE=true
  17. - affinity:service!=master
  18. ports:
  19. - 3306
  20. links:
  21. - master:mysql

Compose V3

In this example, affinity:service!=web is the deployment constraint for the mysql service. In this way, the mysql service is always deployed to nodes where the web service is not deployed. Therefore, when a node is invalid, the service availability will not be affected. When your cluster has only one node, as hard anti-affinity is specified, the deployment will fail because it cannot meet the specified mandatory constraints.

Orchestration example:

  1. version: '3'
  2. services:
  3. web:
  4. image: wordpress:4
  5. environment:
  6. - WORDPRESS_DB_PASSWORD=password
  7. - WORDPRESS_AUTH_KEY=changeme
  8. - WORDPRESS_SECURE_AUTH_KEY=changeme
  9. - WORDPRESS_LOGGED_IN_KEY=changeme
  10. - WORDPRESS_NONCE_KEY=changeme
  11. - WORDPRESS_AUTH_SALT=changeme
  12. - WORDPRESS_SECURE_AUTH_SALT=changeme
  13. - WORDPRESS_LOGGED_IN_SALT=changeme
  14. - WORDPRESS_NONCE_SALT=changeme
  15. - WORDPRESS_NONCE_AA=changeme
  16. ports:
  17. - 80
  18. deploy:
  19. replicas: 3
  20. restart_policy:
  21. condition: on-failure
  22. labels:
  23. aliyun.routing.port_80: wordpress
  24. mysql:
  25. image: mysql:5.7
  26. environment:
  27. - MYSQL_ROOT_PASSWORD=password
  28. - affinity:service!=web
  29. deploy:
  30. restart_policy:
  31. condition: on-failure
Thank you! We've received your feedback.