{"record":{"id":"fd4c2a48f2fed4f7","repo":"derailed/k9s","slug":"unable-to-locate-container-named-q","errorCode":null,"errorMessage":"unable to locate container named %q","messagePattern":"unable to locate container named %q","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/view/container.go","lineNumber":241,"sourceCode":"\t}\n\tif cs == nil {\n\t\treturn fmt.Errorf(\"unable to locate container status for %q\", co)\n\t}\n\n\tif render.ToContainerState(cs.State) != \"Running\" {\n\t\treturn fmt.Errorf(\"Container %s is not running?\", co)\n\t}\n\n\treturn nil\n}\n\nfunc locateContainer(co string, cc []v1.Container) (*v1.Container, error) {\n\tfor i := range cc {\n\t\tif cc[i].Name == co {\n\t\t\treturn &cc[i], nil\n\t\t}\n\t}\n\treturn nil, fmt.Errorf(\"unable to locate container named %q\", co)\n}\n\nfunc (c *Container) listForwardable(path string) (port.ContainerPortSpecs, map[string]string, bool) {\n\tpo, err := fetchPod(c.App().factory, c.GetTable().Path)\n\tif err != nil {\n\t\treturn nil, nil, false\n\t}\n\n\tco, err := locateContainer(path, po.Spec.Containers)\n\tif err != nil {\n\t\tc.App().Flash().Err(err)\n\t\treturn nil, nil, false\n\t}\n\n\tif err := checkRunningStatus(path, po.Status.ContainerStatuses); err != nil {\n\t\tc.App().Flash().Err(err)\n\t\treturn nil, nil, false\n\t}","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/derailed/k9s/blob/2d3ccc6ba2ce98c3781bfc441bb3e884f072774f/internal/view/container.go#L223-L259","documentation":"locateContainer searches po.Spec.Containers for the given name and found no match. The container name is not declared in the pod spec's main containers list, so specs like ports cannot be located. Init and ephemeral containers are excluded from this slice, which is the usual root cause.","triggerScenarios":"listForwardable (port-forward setup) or similar paths called with the name of an init container, ephemeral container, or a typo'd/mismatched name; a stale pod object from cache after the spec changed (containers renamed in a rollout); case-sensitive mismatch ('App' vs 'app').","commonSituations":"Port-forwarding to sidecars defined as init containers; debug ephemeral containers added post-creation; views holding cached pods across a deployment update; copy-paste of container names with wrong case.","solutions":["Use a name from Spec.Containers: kubectl get pod <pod> -o jsonpath='{.spec.containers[*].name}'.","If the target is an init/ephemeral container, extend the search to Spec.InitContainers / Spec.EphemeralContainers (note port-forward generally requires a running main container anyway).","Refresh the pod (watch/re-fetch) so the spec matches the live object before acting on it.","Check exact casing — matching is exact string equality."],"exampleFix":"// before\nco, err := locateContainer(path, po.Spec.Containers)\n\n// after: fall back to init containers before failing\nco, err := locateContainer(path, po.Spec.Containers)\nif err != nil {\n    co, err = locateContainer(path, po.Spec.InitContainers)\n}","handlingStrategy":"validation","validationCode":"co, err := locateContainer(path, po.Spec.Containers)\nif err != nil {\n    if co, err = locateContainer(path, po.Spec.InitContainers); err != nil {\n        return fmt.Errorf(\"container %q not in spec (main/init/ephemeral)\", path)\n    }\n}","typeGuard":"func containerInSpec(cc []v1.Container, name string) (*v1.Container, bool) {\n    for i := range cc {\n        if cc[i].Name == name { return &cc[i], true }\n    }\n    return nil, false\n}","tryCatchPattern":"if err := locateContainer(path, po.Spec.Containers); err != nil {\n    if strings.Contains(err.Error(), \"unable to locate container named\") {\n    // re-fetch pod, verify name casing, and check init/ephemeral lists before giving up\n    }\n}","preventionTips":["Forward ports only to containers in spec.containers; init containers are usually not forwardable.","Re-fetch the pod before acting so renamed containers (rollouts) don't cause stale lookups.","Source names from a fresh pod spec and match case exactly."],"tags":["kubernetes","pods","containers","port-forward"],"backgroundTag":null,"analyzedSha":"2d3ccc6ba2ce98c3781bfc441bb3e884f072774f","analyzedAt":"2026-08-15T16:09:14.432Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}