How to Deploy a Container into a Specific Node in a Docker Swarm

can we deploy a container into a specific node in a docker swarm

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.

Run docker container on a specific node in the swarm cluster

You are using an older classic Swarm method to try running your container, but almost certainly using Swarm Mode. If you installed your swarm with docker swarm init and can see nodes with docker node ls on the manager, then this is the case.

Classic Swarm ran as a container that was effectively a reverse proxy to multiple docker engines, intercepting calls to commands like docker run and sending them to the appropriate node. It is generally recommended to avoid this older swarm implementation unless you have a specific use case and take the time to configure mTLS on each of your docker hosts.

Swarm Mode provides an HA manager using Raft for quorum (same as etcd), handles encryption of all management requests, configures overlay networking, and works with a declarative model, giving the target state, rather than imperative commands to run. It's a very different model from classic Swarm. Notably, Swarm Mode only works on services and stacks, and completely ignores the docker run and other local commands, e.g.:

docker service create \
--name backup \
--constraint node.hostname==storageBeta \
--network cluster_02 \
-v "/var/lib:/srv/toBackup" \
-e BACKUPS_TO_KEEP="5" \
-e S3_BUCKET="backup" \
-e S3_ACCESS_KEY="" \
-e S3_SECRET_KEY="" \
comp/backup create $BACKUP_NAME

Note that jobs are not well supported in Swarm Mode yet (there is an open issue seeking community feedback on including this functionality). It is currently focused on running persistent services that do not normally exit unless there is an error. If your command is expected to exit, you can include an option like --restart-max-attempts 0 to prevent swarm mode from restarting it. You may also have additional work to do if the network is not swarm scoped.

I'd also recommend converting this to a docker-compose.yml file and deploying with docker stack deploy to better document the service as a config file.

How to deploy container to subset of worker nodes in a docker swarm using affinity?

You can tag the 2 worker nodes:

docker node update --label-add foo=bar node-1
docker node update --label-add foo=bar node-2

and then use deploy placement constraints to force the container land on the nodes that you labeled in the previous step:

services:
your-service:
image: your-image
deploy:
placement:
constraints: [node.labels.foo=bar ]

docker-compose swarm: force containers to run on specific hosts

My first answer is about "swarm mode". You'd since clarified that you're using legacy Swarm and added more info, so here:

The constraint you list assumes that you have a host named node-1. Your hosts are named ux-test16.rs and ux-test17.rs. Just use that instead of node-1 in your constraint. Eg:

environment:
- "constraint:node==ux-test16.rs"

how to limit container running on different node use docker stack deploy

To start a service on each available node in your Docker Swarm cluster you need to run it in global mode.

But, in your case because of the specific volumes for each Zookeeper you can use placement constraints to control the nodes a service can be assigned to. So you can add the following section to each Zookeeper service which will allow each instance to run on a different node:

services:
...
zookeeper-1:
...
deploy:
placement:
constraints:
- node.hostname==node1

docker swarm: how to publish a service only on a specific node that runs a task

I don't know how did you specify host mode since your docker-compose.yml doesn't represent host mode anywhere.

Incidentally, try with long syntax which can specify host mode in docker-compose.yml file.

This long syntax is new in v3.2 and the below is the example(I check it works)

(This is compatible docker engine version against docker-compose syntax version.)

version: '3.4'     # version: '3.2' also will works

networks:
swarm_network:
driver: overlay

services:
service1:
image: asleea/test

command: ["nc", "-vlkp", "8000"]

deploy:
mode: replicated
replicas: 1
placement:
constraints:
- node.hostname == node1

ports:
- published: 8000
target: 8000
mode: host

networks:
swarm_network:

service2:
image: asleea/test

command: ["nc", "service1", "8000"]

deploy:
mode: replicated
replicas: 1
placement:
constraints:
- node.hostname == node2

ports:
- published: 8000
target: 8000
mode: host

networks:
swarm_network:


Related Topics



Leave a reply



Submit