Misuse of shell builtins or incorrect usage
Production Risk
Indicates a configuration or scripting defect that will cause repeated restarts.
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.
- 1Shell script entrypoint passes invalid flags to a built-in command
- 2Application uses exit code 2 to signal invalid CLI arguments
- 3diff or similar tool exits 2 to indicate an error (vs. 1 for differences found)
Container exits immediately after start with code 2, usually indicating a scripting or CLI argument problem.
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
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
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.
Kubernetes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev