{"record":{"id":"4b03404f81545a16","repo":"derailed/k9s","slug":"container-s-is-not-running","errorCode":null,"errorMessage":"Container %s is not running?","messagePattern":"Container (.+?) is not running\\?","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/view/container.go","lineNumber":229,"sourceCode":"\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\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","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/derailed/k9s/blob/2d3ccc6ba2ce98c3781bfc441bb3e884f072774f/internal/view/container.go#L211-L247","documentation":"The container was found in the pod's status, but render.ToContainerState(cs.State) is not \"Running\" — it is Waiting (Pending/ImagePullBackOff/CrashLoopBackOff) or Terminated. The operation being attempted (exec, attach, port-forward, log streaming) requires a running container.","triggerScenarios":"Exec/attach into a container stuck in ImagePullBackOff or CrashLoopBackOff; a pod still Pending with containers Waiting (ContainerCreating); a container that just Terminated (completed Job pod); racing a restart loop where state flips between checks.","commonSituations":"Bad image reference or registry auth failure; crash-looping app under debugging; Jobs completing before interaction; probes failing so the container restarts constantly; acting on a pod snapshot taken before first start.","solutions":["Inspect why it is not running: kubectl describe pod <pod> and kubectl logs --previous for events/crash causes.","Fix the underlying cause (image name, registry secret, app crash, probes).","Wait for Running: kubectl wait --for=condition=Ready pod/<pod> --timeout=120s, then retry.","For completed Job containers, accept that exec/attach is impossible; use logs instead."],"exampleFix":"# before: exec immediately\nkubectl exec -it pod/demo -c app -- sh\n\n# after: wait for running, then exec\nkubectl wait --for=condition=Ready pod/demo --timeout=120s\nkubectl exec -it pod/demo -c app -- sh","handlingStrategy":"validation","validationCode":"cs, ok := containerStatusNamed(po.Status.ContainerStatuses, co)\nif !ok { return fmt.Errorf(\"no status for %q\", co) }\nif render.ToContainerState(cs.State) != \"Running\" {\n    return fmt.Errorf(\"container %s is %s (waiting: %s) — wait for Running\", co, render.ToContainerState(cs.State), waitingReason(cs))\n}","typeGuard":"func isContainerRunning(ss []v1.ContainerStatus, name string) bool {\n    for i := range ss {\n        if ss[i].Name == name { return render.ToContainerState(ss[i].State) == \"Running\" }\n    }\n    return false\n}","tryCatchPattern":"if err := checkRunningStatus(co, ss); err != nil {\n    if strings.Contains(err.Error(), \"not running\") {\n    describePodAndShowEvents(po) // guide to root cause: image, crash, probes\n    }\n}","preventionTips":["Gate exec/attach/port-forward UI on container state == Running from live status.","For crash-looping containers use logs --previous instead of exec.","After rollout/restart, wait on Ready condition before interacting."],"tags":["kubernetes","pods","containers","lifecycle","debugging"],"backgroundTag":null,"analyzedSha":"2d3ccc6ba2ce98c3781bfc441bb3e884f072774f","analyzedAt":"2026-08-15T16:09:14.432Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}