19

I have a docker swarm cluster, it contains 1 master 3 nodes. When we deploy a container through swarm master, e.g with the below command

docker -H tcp://<master_ip>:5001 run -dt --name swarm-test busybox /bin/sh

Swarm will auto pick a node and deploy my container. Is there a way to hand pick a node? e.g I want to deploy a container in node 1.

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
jojo
  • 13,583
  • 35
  • 90
  • 123

2 Answers2

22

Take a look at the Swarm filter docs. You can set various constraints on what node Swarm should pick for any given container. For your case try something like:

docker run ... -e constraint:node==node1 ...

This would start the container on node1.

Kalsan
  • 822
  • 1
  • 8
  • 19
Erik Dannenberg
  • 5,716
  • 2
  • 16
  • 21
  • what is "node1"? a hostname..? i need to actually start it on node1 but docker doesn't understand what is node1 – holms Aug 10 '17 at 09:48
  • ok so it's first part of hostname actually so I had to extract it with terraform: maybe someone will be interested: `sudo docker --tls -H ${aws_instance.master.0.private_ip}:2376 node update --label-add name=slave-${count.index} ${element(split(".", self.private_dns), 0)}` – holms Aug 15 '17 at 08:04
  • 2
    https://docs.docker.com/v17.12/engine/reference/commandline/service_create/#set-service-mode-mode – Stanislav Mamontov Jun 09 '18 at 09:21
0

Simple nginx example (using hostname of server)

docker service create --publish {serverPort}:80 --constraint "node.hostname == {hostName}" --name {serviceName} nginx:latest