23

After executing this;

eval $(docker-machine env mymachine)

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

Ernest Okot
  • 880
  • 3
  • 8
  • 23

6 Answers6

39

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
BMitch
  • 231,797
  • 42
  • 475
  • 450
  • This is a nice answer and the solution that fits best. – Carl Wainwright Jul 22 '19 at 18:35
  • This worked great for me, but I had it in a bootstrap script and found it a bit slow and therefore needed a more efficient method of accessing the value. I came up with `docker node inspect self --format '{{.Spec.Availability}}'` – Adrian Günter Sep 17 '22 at 01:01
29

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
}
  • 1
    This is a nice function. I just think you may add a redirection 2>/dev/null after 'docker info', in order to prevent docker warnings to pile up with your true/false response – Marvin Apr 17 '19 at 12:48
  • This is a great function for some automation I'm writing. – joshmcode Apr 26 '19 at 22:48
  • 10
    [`docker info --format '{{.Swarm.LocalNodeState}}'`](https://stackoverflow.com/a/50590287/52499) solution seems to be more in order. – x-yuri Jul 01 '19 at 23:32
9

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
larsks
  • 277,717
  • 41
  • 399
  • 399
  • 2
    As @MarcelloRomani points out, this test fails in worker nodes. As written in https://docs.docker.com/get-started/part4/ , there are 2 types swarm node; manager node & worker node. And worker node cannot execute "docker node" (and some other swarm related) command even though it's swarm node. So, I recommend to see "docker info" command output. – Shinbero Feb 16 '19 at 11:33
0

In addition to larsks answer, if you run docker node ls when pointing to a worker node, you'll get the following message:

Error response from daemon: This node is not a swarm manager. Worker nodes can't be used to view or modify cluster state. Please run this command on a manager node or promote the current node to a manager.

You can use this to differentiate between worker nodes and nodes not in a swarm at all.

TerekC
  • 2,133
  • 3
  • 20
  • 22
0

Make sure your docker environment variables are set properly

$env | grep DOCKER_

Compare url and port values with the output from

$docker-machine ls

Select the swarm master machine name and you can reset the environment variables using

$eval $(docker-machine env your_master_machine_name)

Once environment variables are set properly, your command

$docker info | egrep '^Swarm: ' | cut -d ' ' -f2

should give the correct result

Raman Garg
  • 91
  • 1
  • 2
0

To get the IP address of a manager from any node (either worker or manager) using bash you can do:

read manager_ip _ <<<$(IFS=':'; echo $(docker info --format "{{ (index .Swarm.RemoteManagers 0).Addr }}"))
echo "${manager_ip}"

As mentioned above, the most direct way to identify if the current node is a manager is by using:

docker info --format '{{.Swarm.ControlAvailable}}'
b f
  • 41
  • 1
  • 2