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

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

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
}

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