{"record":{"id":"ff7e0519246296e2","repo":"GoogleContainerTools/skaffold","slug":"hook-execution-failed-for-pod-q-container-q-w","errorCode":null,"errorMessage":"hook execution failed for pod %q container %q: %w","messagePattern":"hook execution failed for pod %q container %q: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/skaffold/hooks/container.go","lineNumber":125,"sourceCode":"\t\t\tfor _, c := range p.Spec.Containers {\n\t\t\t\tif matched, err := h.selector(p, c); err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t} else if !matched {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\targs := []string{p.Name, \"--namespace\", p.Namespace, \"-c\", c.Name, \"--\"}\n\t\t\t\targs = append(args, h.cfg.Command...)\n\t\t\t\tcmd := h.cli.Command(ctx, \"exec\", args...)\n\t\t\t\ttr, tw := io.Pipe()\n\t\t\t\tcmd.Stderr = tw\n\t\t\t\tcmd.Stdout = tw\n\t\t\t\tpodName := p.Name\n\t\t\t\tcontainerName := c.Name\n\t\t\t\terrs.Go(func() error {\n\t\t\t\t\tdefer tw.Close()\n\t\t\t\t\terr := util.RunCmd(ctx, cmd)\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn fmt.Errorf(\"hook execution failed for pod %q container %q: %w\", podName, containerName, err)\n\t\t\t\t\t}\n\t\t\t\t\treturn nil\n\t\t\t\t})\n\t\t\t\tpod := p\n\t\t\t\tvar containerStatus v1.ContainerStatus\n\t\t\t\tfor _, status := range pod.Status.ContainerStatuses {\n\t\t\t\t\tif status.Name == c.Name {\n\t\t\t\t\t\tcontainerStatus = status\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\terrs.Go(func() error {\n\t\t\t\t\treturn stream.StreamRequest(ctx, out, h.formatter(pod, containerStatus, func() bool { return false }), tr)\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\t}\n\treturn errs.Wait()\n}","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/GoogleContainerTools/skaffold/blob/a1189de023efc32d4b8e11f395acc678aa555011/pkg/skaffold/hooks/container.go#L107-L143","documentation":"Once a matching pod/container is found, the hook command runs via util.RunCmd in an errgroup goroutine. If the command exits non-zero or fails to run, the error is wrapped as 'hook execution failed for pod %q container %q: %w', attributing the failure to the specific pod and container.","triggerScenarios":"run dispatching errs.Go for a matched container where util.RunCmd returns an error — the hook command binary is missing in the container, exits non-zero, or exec into the pod fails.","commonSituations":"Hook command not installed in the target container image; script exits with a non-zero status due to app logic; insufficient permissions to exec into the pod; container restarted/terminated mid-hook; wrong command name in skaffold.yaml.","solutions":["Check the wrapped stderr/stdout in the error to see the actual command failure","Verify the command exists in the container image (kubectl exec <pod> -c <ctr> -- which <cmd>)","Fix the command/args in the skaffold.yaml hook definition","Ensure the container stays Running during the hook and the app exits 0"],"exampleFix":"// before\ncontainerHooks:\n  postBuild:\n    - command: [\"./scripts/notify.sh\"]   # not present in image\n// after\ncontainerHooks:\n  postBuild:\n    - command: [\"/bin/sh\", \"-c\", \"[ -f /scripts/notify.sh ] && /scripts/notify.sh || true\"]","handlingStrategy":"try-catch","validationCode":"// ensure the command exists in the image before running the hook\nif err := exec.Command(\"kubectl\", \"exec\", pod, \"-c\", container, \"--\", \"which\", cmdName).Run(); err != nil {\n    return fmt.Errorf(\"command %q not present in %s/%s\", cmdName, pod, container)\n}","typeGuard":null,"tryCatchPattern":"err := hook.Run(ctx, out)\nvar exitErr *exec.ExitError\nif err != nil {\n    if errors.As(err, &exitErr) {\n        log.Printf(\"hook failed in pod/container (exit %d): %v\", exitErr.ExitCode(), err)\n    }\n    return err\n}","preventionTips":["Bake hook commands into the container image","Make hook scripts idempotent and exit 0 on benign conditions","Check pod Running status before dispatching hooks","Capture and read the wrapped command output when debugging"],"tags":["exec","kubernetes","hooks","command-failed"],"backgroundTag":"hook-command-exit-nonzero","analyzedSha":"a1189de023efc32d4b8e11f395acc678aa555011","analyzedAt":"2026-09-05T12:09:27.064Z","contentChangedAt":"2026-09-05T12:09:27.064Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}