ExitCode 2
KubernetesERRORNotableContainer ErrorHIGH confidence

Misuse of shell builtins or incorrect usage

Production Risk

Indicates a configuration or scripting defect that will cause repeated restarts.

What this means

Exit code 2 typically indicates a misuse of shell built-in commands, incorrect command-line argument syntax, or a bash scripting error. It is commonly produced by bash itself, tools like diff, grep, or getopt when called with invalid arguments, or when a container entrypoint script calls exit 2 on bad input.

Why it happens
  1. 1Shell script entrypoint passes invalid flags to a built-in command
  2. 2Application uses exit code 2 to signal invalid CLI arguments
  3. 3diff or similar tool exits 2 to indicate an error (vs. 1 for differences found)
How to reproduce

Container exits immediately after start with code 2, usually indicating a scripting or CLI argument problem.

trigger — this will error
trigger — this will error
kubectl describe pod mypod
# Last State: Terminated  Reason: Error  Exit Code: 2

kubectl logs mypod --previous

expected output

Last State:     Terminated
  Reason:       Error
  Exit Code:    2

Fix 1

Review the container command and args

WHEN Exit code 2 occurs on startup

Review the container command and args
kubectl describe pod mypod | grep -A 10 "Command:"
# Check args passed in the pod spec
kubectl get pod mypod -o jsonpath='{.spec.containers[0].command}'
kubectl get pod mypod -o jsonpath='{.spec.containers[0].args}'

Why this works

Identifies mismatched or invalid arguments passed to the entrypoint.

Fix 2

Run container interactively to reproduce

WHEN The script behaviour is unclear

Run container interactively to reproduce
kubectl run debug --image=myimage:tag --restart=Never -it --rm -- /bin/sh

Why this works

Provides an interactive shell to test the entrypoint command manually.

Sources
Official documentation ↗

Kubernetes Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Kubernetes errors