Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check that a docker host is in swarm mode?

After executing this;

eval $(docker-machine env mymachine)

How do I check if the docker daemon on mymachine is a swarm manager?

like image 675
Ernest Okot Avatar asked Mar 27 '17 17:03

Ernest Okot


People also ask

Which command is used to view the hostname of docker swarm cluster computers?

You can find the manager hostname using the docker node ls command.

What is docker in swarm mode?

Docker Engine 1.12 introduces swarm mode that enables you to create a cluster of one or more Docker Engines called a swarm. A swarm consists of one or more nodes: physical or virtual machines running Docker Engine 1.12 or later in swarm mode. There are two types of nodes: managers and workers.

Where can I find docker swarm manager?

To find out manager nodes, run docker info It lists out current node address as well as manager addresses, among many other info.


3 Answers

To check general swarm membership, my preferred method is to use the formatted output from docker info. The possible values of this are currently inactive, pending, active, locked, and error:

case "$(docker info --format '{{.Swarm.LocalNodeState}}')" in
  inactive)
    echo "Node is not in a swarm cluster";;
  pending)
    echo "Node is not in a swarm cluster";;
  active)
    echo "Node is in a swarm cluster";;
  locked)
    echo "Node is in a locked swarm cluster";;
  error)
    echo "Node is in an error state";;
  *)
    echo "Unknown state $(docker info --format '{{.Swarm.LocalNodeState}}')";;
esac

To check for manager status, rather than just a node in a cluster, the field you want is .Swarm.ControlAvailable:

docker info --format '{{.Swarm.ControlAvailable}}'

That will output "true" for managers, and "false" for any node that is a worker or not in a swarm.

To identify worker nodes, you can join to two:

if [ "$(docker info --format '{{.Swarm.LocalNodeState}}')" = "active" \
     -a "$(docker info --format '{{.Swarm.ControlAvailable}}')" = "false" ]; then
  echo "node is a worker"
else
  echo "node is not a worker"
fi
like image 187
BMitch Avatar answered Sep 23 '22 22:09

BMitch


You could also use docker info to see the result of Swarm property (inactive or active).

For example:

function isSwarmNode(){
    if [ "$(docker info | grep Swarm | sed 's/Swarm: //g')" == "inactive" ]; then
        echo false;
    else
        echo true;
    fi
}
like image 45
Mauro Cortellazzi Avatar answered Sep 22 '22 22:09

Mauro Cortellazzi


I don't have a swarm node handy at the moment, but it looks as if you could simply run something like docker node ls. When targeting a docker daemon that is not in swarm node, that results in:

Error response from daemon: This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.

And it returns a nonzero exit code

$ echo $?
1

So the test would look something like:

if docker node ls > /dev/null 2>&1; then
  echo this is a swarm node
else
  echo this is a standalone node
fi
like image 32
larsks Avatar answered Sep 19 '22 22:09

larsks