Tuesday, January 26, 2016

Docker: How to start a container on a specific node in a Swarm cluster?

If you are using a Docker Swarm cluster and are wondering how you can start a container on a specific node, this post is for you!

The short answer is to custom label the docker node(daemon):

[root@hdp1 ~]# cat /etc/sysconfig/docker 
OPTIONS="-H tcp:// --label nodename=n1.manfrix.com"
[root@hdp1 ~]# 
NOTE: If you want to know more about the "label" option of docker daemon, you can refer here.

Make sure you restart the docker service after the changes:

systemctl restart docker

Verify that the daemon is now started with a label:

[root@hdp1 ~]# ps -ef | grep docker
root      50961      1  0 20:38 ?        00:00:02 /usr/bin/docker daemon -H fd:// -H tcp:// --label nodename=n1.manfrix.com

Make sure you remember to re-join the nodes to the swarm cluster after you restarted docker:

docker run -d swarm join --addr=[IP of the node]:2375 etcd://[IP of etcd host]:[port]/[optional path prefix]

After you have labeled all the nodes (daemons), then you can proceed to test:

[root@hdp1 ~]# docker -H tcp://localhost:9999 run -d --name centos-1 -p 80 -e constraint:nodename==n3.manfrix.com centos /bin/bash
[root@hdp1 ~]# docker -H tcp://localhost:9999 run -d --name nginx-1 -p 80 -e constraint:nodename==n2.manfrix.com nginx

For more information about Swarm node constraints, you can refer here.

No comments:

Post a Comment