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

CC BY-SA 4.0
2021-10-12T23:27:53.530Z
2021-10-12T23:27:53.530Z
BMitch
<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>

<docker><docker-compose><docker-swarm>
Docker Swarm set which service goes to which machine
Krishna Torque
# 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 of the compose file, you can specify constraints. 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"


---

