{"record":{"id":"c186c129661d42e5","repo":"derailed/k9s","slug":"unable-to-locate-container-status-for-q","errorCode":null,"errorMessage":"unable to locate container status for %q","messagePattern":"unable to locate container status for %q","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/view/container.go","lineNumber":225,"sourceCode":"\tports, ann, ok := c.listForwardable(path)\n\tif !ok {\n\t\treturn nil\n\t}\n\tShowPortForwards(c, c.GetTable().Path+\"|\"+path, ports, ann, startFwdCB)\n\n\treturn nil\n}\n\nfunc checkRunningStatus(co string, ss []v1.ContainerStatus) error {\n\tvar cs *v1.ContainerStatus\n\tfor i := range ss {\n\t\tif ss[i].Name == co {\n\t\t\tcs = &ss[i]\n\t\t\tbreak\n\t\t}\n\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","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/derailed/k9s/blob/2d3ccc6ba2ce98c3781bfc441bb3e884f072774f/internal/view/container.go#L207-L243","documentation":"checkRunningStatus scans the pod's Status.ContainerStatuses for a container by name and found none. The name being checked is not present in the pod's reported status list, so its state cannot be determined. Distinguish from a pod-spec problem: the container may exist in spec but be missing from status.","triggerScenarios":"Calling exec/attach/log-follow paths guarded by checkRunningStatus for a container that is an initContainer or ephemeralContainer — those live in Status.InitContainerStatuses/Status.EphemeralContainerStatuses, not ContainerStatuses; checking immediately after pod creation before the kubelet has reported status (status list empty or partial); a name mismatch (typo, different casing).","commonSituations":"Sidecars implemented as init containers (restartPolicy: Always) where the name only appears in init status; ephemeral debug containers; racing pod creation in controllers/tests; renamed containers during rollout while holding a stale name.","solutions":["Pass a name from Status.ContainerStatuses; for init/ephemeral containers, check Status.InitContainerStatuses or Status.EphemeralContainerStatuses instead.","Wait/retry until the kubelet populates status (kubectl get pod -w or a watch) before invoking container operations.","Verify the exact name: kubectl get pod <pod> -o jsonpath='{.status.containerStatuses[*].name}'.","Refresh the view — a stale cached pod may no longer match the live object."],"exampleFix":"// before\nif err := checkRunningStatus(co, po.Status.ContainerStatuses); err != nil { ... }\n\n// after: search the right status list per container kind\nss := po.Status.ContainerStatuses\nif !inList(co, ss) { ss = po.Status.InitContainerStatuses }\nif !inList(co, ss) { ss = po.Status.EphemeralContainerStatuses }\nif err := checkRunningStatus(co, ss); err != nil { ... }","handlingStrategy":"validation","validationCode":"func statusListFor(po *v1.Pod, co string) []v1.ContainerStatus {\n    if inStatus(po.Status.ContainerStatuses, co) { return po.Status.ContainerStatuses }\n    if inStatus(po.Status.InitContainerStatuses, co) { return po.Status.InitContainerStatuses }\n    return po.Status.EphemeralContainerStatuses\n}\nif err := checkRunningStatus(co, statusListFor(po, co)); err != nil { return err }","typeGuard":"func containerStatusNamed(ss []v1.ContainerStatus, name string) (v1.ContainerStatus, bool) {\n    for i := range ss {\n        if ss[i].Name == name { return ss[i], true }\n    }\n    return v1.ContainerStatus{}, false\n}","tryCatchPattern":"if err := checkRunningStatus(co, ss); err != nil {\n    if strings.Contains(err.Error(), \"unable to locate container status\") {\n    // refresh pod and select the status list matching the container kind before retrying once\n    }\n}","preventionTips":["Pick container names from the pod's status (not spec) and from the list matching the container kind (main/init/ephemeral).","Watch pods until status is populated before enabling interactive container commands.","Compare names exactly — matching is case-sensitive string equality."],"tags":["kubernetes","pods","containers","status-mismatch"],"backgroundTag":null,"analyzedSha":"2d3ccc6ba2ce98c3781bfc441bb3e884f072774f","analyzedAt":"2026-08-15T16:09:14.432Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}