docker-compose swarm: force containers to run on specific hosts
Solution 1
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"
Solution 2
The environment variable constraint
is only valid for the legacy (stand alone) version of Swarm. The newer "Swarm Mode" uses either mode
or constraints
options (not environment variables).
To enforce one and only one task (container) per node, use mode=global
.
docker service create --name proxy --mode global nginx
The default mode is replicated
which means that the swarm manager will create tasks (containers) across all available nodes to meet the number specified in the --replicas
option. Eg:
docker service create --name proxy --replicas 5 nginx
To enforce other constraints based on hostname (node), label, role, id's use the --constraint
option. Eg:
docker service create --name proxy --constraint "node.hostname!=node01" nginx
See https://docs.docker.com/engine/reference/commandline/service_create/#/specify-service-constraints
EDIT sept 2016:
Something else. docker-compose
is not currently supported in "swarm mode". Swarm mode understands the new dab
format instead. There is a way to convert docker-compose files to dab but it's experimental and not to be relied on at this point. It's better to create a bash script that calls all the docker service create ...
directly.
EDIT March 2017:
As of docker 1.13 (17.03), docker-compose can now be used to provision swarm environments directly without having to deal with the dab step.
user3292147
Updated on June 17, 2022Comments
-
user3292147 almost 2 years
Trying to run cluster application on different virtual machines with use of Swarm stand alone and docker-compose version '2'. Overlay network is set. But want to force certain containers to run on specific hosts.
In documentation there is following advice, but with this parameter I was not able to start any container at all:
environment: - "constraint:node==node-1" ERROR: for elasticsearch1 Cannot create container for service elasticsearch1: Unable to find a node that satisfies the following conditions [available container slots] [node==node-1]
Should we register hosts as node-1 node-2... or it is done by default.
[root@ux-test14 ~]# docker node ls Error response from daemon: 404 page not found [root@ux-test14 ~]# docker run swarm list [root@ux-test14 ~]# [root@ux-test14 ~]# docker info Containers: 8 Running: 6 Paused: 0 Stopped: 2 Images: 8 Server Version: swarm/1.2.5 Role: primary Strategy: spread Filters: health, port, containerslots, dependency, affinity, constraint Nodes: 2 ux-test16.rs: 10.212.212.2:2375 â ID: JQPG:GKFF:KJZJ:AY3N:NHPZ:HD6J:SH36:KEZR:2SSH:XF65:YW3N:W4DG â Status: Healthy â Containers: 4 (4 Running, 0 Paused, 0 Stopped) â Reserved CPUs: 0 / 2 â Reserved Memory: 0 B / 3.888 GiB â Labels: kernelversion=3.10.0-327.28.3.el7.x86_64, operatingsystem=CentOS Linux 7 (Core), storagedriver=devicemapper â UpdatedAt: 2016-09-05T11:11:31Z â ServerVersion: 1.12.1 ux-test17.rs: 10.212.212.3:2375 â ID: Z27V:T5NU:QKSH:DLNK:JA4M:V7UX:XYGH:UIL6:WFQU:FB5U:J426:7XIR â Status: Healthy â Containers: 4 (2 Running, 0 Paused, 2 Stopped) â Reserved CPUs: 0 / 2 â Reserved Memory: 0 B / 3.888 GiB â Labels: kernelversion=3.10.0-327.28.3.el7.x86_64, operatingsystem=CentOS Linux 7 (Core), storagedriver=devicemapper â UpdatedAt: 2016-09-05T11:11:17Z â ServerVersion: 1.12.1 Plugins: Volume: Network: Swarm: NodeID: Is Manager: false Node Address: Security Options: Kernel Version: 3.10.0-327.28.3.el7.x86_64 Operating System: linux Architecture: amd64 CPUs: 4 Total Memory: 7.775 GiB Name: 858ac2fdd225 Docker Root Dir: Debug Mode (client): false Debug Mode (server): false WARNING: No kernel memory limit support