docker/cli · error

cannot get label com.docker.stack.namespace for service {svc

Error message

cannot get label com.docker.stack.namespace for service {svc.ID}

What it means

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.

Source

Thrown at cli/command/stack/list_utils.go:26

	"github.com/moby/moby/client"
)

// getStacks lists the swarm stacks with the number of services they contain.
func getStacks(ctx context.Context, apiClient client.ServiceAPIClient) ([]stackSummary, error) {
	res, err := apiClient.ServiceList(ctx, client.ServiceListOptions{
		Filters: getAllStacksFilter(),
	})
	if err != nil {
		return nil, err
	}

	idx := make(map[string]int, len(res.Items))
	out := make([]stackSummary, 0, len(res.Items))

	for _, svc := range res.Items {
		name, ok := svc.Spec.Labels[convert.LabelNamespace]
		if !ok {
			return nil, errors.New("cannot get label " + convert.LabelNamespace + " for service " + svc.ID)
		}
		if i, ok := idx[name]; ok {
			out[i].Services++
			continue
		}
		idx[name] = len(out)
		out = append(out, stackSummary{Name: name, Services: 1})
	}
	return out, nil
}

View on GitHub (pinned to 4f84911bfe)

Solutions

  1. Add the missing label: `docker service update --label-add com.docker.stack.namespace=<stack> <svc>`.
  2. Remove the orphaned service if it isn't part of a stack: `docker service rm <svc>`.
  3. Find offending services: `docker service ls --format '{{.ID}} {{.Name}}' ` then `docker service inspect <id>` to check labels.

Example fix

// before: docker stack ls fails because a service lacks the namespace label

// after: attach the label (or remove the orphan)
docker service update --label-add com.docker.stack.namespace=mystack orphan-svc
# or
docker service rm orphan-svc
Defensive patterns

Strategy: validation

Validate before calling

// Ensure every listed service carries the namespace label.
for _, svc := range services.Items {
	if _, ok := svc.Spec.Labels[convert.LabelNamespace]; !ok {
		return fmt.Errorf("service %s missing %s label", svc.ID, convert.LabelNamespace)
	}
}

Prevention

When it happens

Trigger: `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.

Common situations: 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.

Related errors


AI-assisted analysis of docker/cli@4f84911bfe (2026-08-07). Data as JSON: /api/errors/f6ddc8d1991983c0. Report an issue: GitHub.