{"record":{"id":"f6ddc8d1991983c0","repo":"docker/cli","slug":"cannot-get-label-com-docker-stack-namespace-for-se","errorCode":null,"errorMessage":"cannot get label com.docker.stack.namespace for service {svc.ID}","messagePattern":"cannot get label com\\.docker\\.stack\\.namespace for service (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/command/stack/list_utils.go","lineNumber":26,"sourceCode":"\t\"github.com/moby/moby/client\"\n)\n\n// getStacks lists the swarm stacks with the number of services they contain.\nfunc getStacks(ctx context.Context, apiClient client.ServiceAPIClient) ([]stackSummary, error) {\n\tres, err := apiClient.ServiceList(ctx, client.ServiceListOptions{\n\t\tFilters: getAllStacksFilter(),\n\t})\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tidx := make(map[string]int, len(res.Items))\n\tout := make([]stackSummary, 0, len(res.Items))\n\n\tfor _, svc := range res.Items {\n\t\tname, ok := svc.Spec.Labels[convert.LabelNamespace]\n\t\tif !ok {\n\t\t\treturn nil, errors.New(\"cannot get label \" + convert.LabelNamespace + \" for service \" + svc.ID)\n\t\t}\n\t\tif i, ok := idx[name]; ok {\n\t\t\tout[i].Services++\n\t\t\tcontinue\n\t\t}\n\t\tidx[name] = len(out)\n\t\tout = append(out, stackSummary{Name: name, Services: 1})\n\t}\n\treturn out, nil\n}\n","sourceCodeStart":8,"sourceCodeEnd":37,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/cli/command/stack/list_utils.go#L8-L37","documentation":"Thrown by getStacks (cli/command/stack/list_utils.go:26) when ServiceList returns a service whose Spec.Labels map lacks the `com.docker.stack.namespace` label. Stack grouping depends entirely on that label; without it a service cannot be attributed to any stack and the listing aborts rather than silently miscount.","triggerScenarios":"`docker stack ls` when at least one swarm service in the filtered listing is missing the namespace label — e.g. a service created manually with `docker service create` (no stack label) that nevertheless appears in the listing.","commonSituations":"Manually-created swarm services that collide with stack naming/filtering; a label stripped by an external tool or an older API; partial migration where labels weren't applied.","solutions":["Add the missing label: `docker service update --label-add com.docker.stack.namespace=<stack> <svc>`.","Remove the orphaned service if it isn't part of a stack: `docker service rm <svc>`.","Find offending services: `docker service ls --format '{{.ID}} {{.Name}}' ` then `docker service inspect <id>` to check labels."],"exampleFix":"// before: docker stack ls fails because a service lacks the namespace label\n\n// after: attach the label (or remove the orphan)\ndocker service update --label-add com.docker.stack.namespace=mystack orphan-svc\n# or\ndocker service rm orphan-svc","handlingStrategy":"validation","validationCode":"// Ensure every listed service carries the namespace label.\nfor _, svc := range services.Items {\n\tif _, ok := svc.Spec.Labels[convert.LabelNamespace]; !ok {\n\t\treturn fmt.Errorf(\"service %s missing %s label\", svc.ID, convert.LabelNamespace)\n\t}\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always create stack resources via `docker stack deploy` so labels are applied.","Audit for label-stripped services after migrations.","Tag manually-created services with the namespace label if they belong to a stack."],"tags":["docker","swarm","stack","labels","listing"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}