How do I filter docker swarm nodes by label?

2019-05-06 00:20发布

问题:

I'm running docker 1.12.1 and can't get node filtering by label to work. I first add the "test" label:

$ docker node update --label-add test mr-host
mr-host

When I attempt to filter by it, no nodes are shown:

$ docker node ls --filter label=test
ID  HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS

Filtering by other criteria such as name works fine:

$ docker node ls --filter name=mr-host
ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
1c48m4msidbqwx7vj0lwib8ef *  mr-host   Ready   Active        Leader

The label is shown when I inspect the node:

$ docker node inspect mr-host
[
    {
        "ID": "1c48m4msidbqwx7vj0lwib8ef",
        "Version": {
            "Index": 4865874
        },
        "CreatedAt": "2016-10-12T15:20:10.463286132Z",
        "UpdatedAt": "2016-10-13T16:29:03.113522485Z",
        "Spec": {
            "Labels": {
                "test": ""
            },
            "Role": "manager",
            "Availability": "active"
        },
        "Description": {
            "Hostname": "mr-host",
            "Platform": {
                "Architecture": "x86_64",
                "OS": "linux"
            },
            "Resources": {
                "NanoCPUs": 10000000000,
                "MemoryBytes": 67548012544
            },
            "Engine": {
                "EngineVersion": "1.12.1",
                "Plugins": [
                    {
                        "Type": "Network",
                        "Name": "bridge"
                    },
                    {
                        "Type": "Network",
                        "Name": "host"
                    },
                    {
                        "Type": "Network",
                        "Name": "null"
                    },
                    {
                        "Type": "Network",
                        "Name": "overlay"
                    },
                    {
                        "Type": "Volume",
                        "Name": "local"
                    }
                ]
            }
        },
        "Status": {
            "State": "ready"
        },
        "ManagerStatus": {
            "Leader": true,
            "Reachability": "reachable",
            "Addr": "192.168.0.118:2377"
        }
    }
]

What am I doing wrong? I'm pretty sure I've followed the documentation here correctly: https://docs.docker.com/engine/reference/commandline/node_ls/#/id

回答1:

Simple answer: this doesn't work like you'd think it would, but it looks like they might fix it.

Quoting from the Github issue:

The confusion comes from the fact that we have two set of labels:

Node labels (the ones you've been setting through docker node update) Engine labels (the ones that you set when you start docker daemon with --label) Label filtering in docker node ls is looking at engine labels rather than node labels.

We should probably revert to node label filtering rather than engine filtering



回答2:

As I believe you've seen (based on the thumbs up it received), this is a known issue (#27231) with docker swarm.