{"record":{"id":"ae65e80c395f65d0","repo":"hashicorp/nomad","slug":"port-label-s-already-in-use-by-s","errorCode":null,"errorMessage":"Port label %s already in use by %s","messagePattern":"Port label (.+?) already in use by (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nomad/structs/structs.go","lineNumber":7337,"sourceCode":"\t\t\touter := fmt.Errorf(\"Task %s validation failed: %v\", task.Name, err)\n\t\t\tmErr = multierror.Append(mErr, outer)\n\t\t}\n\t}\n\n\treturn mErr.ErrorOrNil()\n}\n\nfunc (tg *TaskGroup) validateNetworks() error {\n\tvar mErr multierror.Error\n\tportLabels := make(map[string]string)\n\t// host_network -> static port tracking\n\tstaticPortsIndex := make(map[string]map[int]string)\n\tcniArgKeys := set.New[string](len(tg.Networks))\n\n\tfor _, net := range tg.Networks {\n\t\tfor _, port := range append(net.ReservedPorts, net.DynamicPorts...) {\n\t\t\tif other, ok := portLabels[port.Label]; ok {\n\t\t\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"Port label %s already in use by %s\", port.Label, other))\n\t\t\t} else {\n\t\t\t\tportLabels[port.Label] = \"taskgroup network\"\n\t\t\t}\n\n\t\t\tif port.Value != 0 {\n\t\t\t\thostNetwork := port.HostNetwork\n\t\t\t\tif hostNetwork == \"\" {\n\t\t\t\t\thostNetwork = \"default\"\n\t\t\t\t}\n\t\t\t\tstaticPorts, ok := staticPortsIndex[hostNetwork]\n\t\t\t\tif !ok {\n\t\t\t\t\tstaticPorts = make(map[int]string)\n\t\t\t\t}\n\t\t\t\t// static port\n\t\t\t\tif other, ok := staticPorts[port.Value]; ok {\n\t\t\t\t\tif !port.IgnoreCollision {\n\t\t\t\t\t\terr := fmt.Errorf(\"Static port %d already reserved by %s\", port.Value, other)\n\t\t\t\t\t\tmErr.Errors = append(mErr.Errors, err)","sourceCodeStart":7319,"sourceCodeEnd":7355,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/nomad/structs/structs.go#L7319-L7355","documentation":"Raised by TaskGroup.validateNetworks when two port entries in the task group's network stanzas share the same label. Nomad tracks port labels in a map; if a label is already claimed ('taskgroup network' by default), the duplicate is rejected so that port-label references (services, checks, connect) stay unambiguous.","triggerScenarios":"A job whose `network { port \"http\" {} }` (or ReservedPorts/DynamicPorts entries) defines the same port label twice — either within one network block or across multiple network blocks in the same task group.","commonSituations":"Merging job files or template snippets that each define common labels like \"http\"/\"metrics\"; auto-generated specs appending a network stanza when one already exists; copy-pasting a network block into a group that already has ports.","solutions":["Search the task group's network blocks for the duplicated label named in the error","Rename one of the duplicate port labels and update any services/tasks referencing it","Remove redundant port entries if they were accidentally duplicated by templating/merge","Run `nomad job validate` after editing to confirm all labels are unique"],"exampleFix":"// before\nnetwork {\n  port \"http\" {}\n  port \"http\" {}\n}\n// after\nnetwork {\n  port \"http\" {}\n  port \"metrics\" {}\n}","handlingStrategy":"validation","validationCode":"func uniquePortLabels(tg *api.TaskGroup) error {\n  seen := map[string]bool{}\n  for _, n := range tg.Networks {\n    for _, p := range append(n.ReservedPorts, n.DynamicPorts...) {\n      if seen[p.Label] { return fmt.Errorf(\"port label %q duplicated in group %s\", p.Label, *tg.Name) }\n      seen[p.Label] = true\n    }\n  }\n  return nil\n}","typeGuard":"func hasUniquePortLabels(tg *api.TaskGroup) bool {\n  seen := map[string]bool{}\n  for _, n := range tg.Networks {\n    for _, p := range append(n.ReservedPorts, n.DynamicPorts...) {\n      if seen[p.Label] { return false }\n      seen[p.Label] = true\n    }\n  }\n  return true\n}","tryCatchPattern":null,"preventionTips":["Keep a single canonical network block per task group","When templating job files, merge network blocks instead of appending","Grep specs for repeated `port \"...\"` labels before submit","Run `nomad job validate` in CI"],"tags":["nomad","network","ports","job-validation"],"backgroundTag":"duplicate-port-label","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}