Answers: <p>In the <a href="https://docs.docker.com/compose/compose-file/compose-file-v3/#placement" rel="nofollow noreferrer">placement section</a> of the compose file, you can specify <a href="https://docs.docker.com/engine/reference/commandline/service_create/#specify-service-constraints---constraint" rel="nofollow noreferrer">constraints</a>. I'd recommend using node labels so you can add more potential nodes (should an existing node fail) without redefining any deployments bound to a node, but you can also specify hostnames. The result looks like:</p>
  <pre><code>version: &quot;3.9&quot;
  services:
    db:
      image: postgres
      deploy:
        placement:
          constraints:
            - &quot;node.hostname==dbhost&quot;
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2021-10-12T23:27:53.530Z
Answers: 2021-10-12T23:27:53.530Z
Answers: BMitch
Body: <p>Docker Swarm set which service goes to which machine.</p>
  <p>Is there any way to set which service or service replicas goes to which worker machine ?</p>
  
Tags: <docker><docker-compose><docker-swarm>
Title: Docker Swarm set which service goes to which machine
UserDisplayName: Krishna Torque
ThreadText: # Docker Swarm set which service goes to which machine
  
  (Asked by: username_0 on 2021-10-12)
  
  Docker Swarm set which service goes to which machine.
  
  Is there any way to set which service or service replicas goes to which worker machine ?
  
  **Tags:** docker, docker-compose, docker-swarm
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-10-12. Score: 4 | Accepted answer
  
  In the [placement section](https://docs.docker.com/compose/compose-file/compose-file-v3/#placement) of the compose file, you can specify [constraints](https://docs.docker.com/engine/reference/commandline/service_create/#specify-service-constraints---constraint). I'd recommend using node labels so you can add more potential nodes (should an existing node fail) without redefining any deployments bound to a node, but you can also specify hostnames. The result looks like:
  
  ```
  version: "3.9"
  services:
    db:
      image: postgres
      deploy:
        placement:
          constraints:
            - "node.hostname==dbhost"
  
  ```
  
  
  ---
  
  