All Products
Document Center

Service deployment constraints (affinity:service)

Last Updated: Nov 28, 2017

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. But 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.


In this example, affinity:service!=db is the deployment constraint for the web service. In this way, the web service is always deployed to nodes where the db service is not deployed. Therefore, when a node is invalid, the service availability is enhanced. 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.

  1. web:
  2. image:
  3. ports:
  4. - '80'
  5. environment:
  6. - affinity:service!=db
  7. restart: always
  8. links:
  9. - 'db:mysql'
  10. labels:
  11. aliyun.logs: /var/log
  12. aliyun.probe.url: http://container/license.txt
  13. aliyun.probe.initial_delay_seconds: '10'
  14. aliyun.routing.port_80: http://wordpress
  15. aliyun.scale: '2'
  16. db:
  17. image:
  18. environment:
  19. MYSQL_ROOT_PASSWORD: password
  20. restart: always
  21. labels:
  22. aliyun.logs: /var/log/mysql