Fix "CrashLoopBackOff" / "Back-off restarting failed container" (Kubernetes)
Back-off restarting failed container app in pod api-7d9f8b6c5d-x2kqp_default(3c1d6f0e-9a2b-4e57-8f1a-6b2d0c9e4a11)
- api-7d9f8b6c5d-x2kqp 0/1 CrashLoopBackOff 6 (2m ago) 9m
- back-off 5m0s restarting failed container=app pod=api-7d9f8b6c5d-x2kqp_default(3c1d6f0e-9a2b-4e57-8f1a-6b2d0c9e4a11)
- Warning BackOff 3m (x28 over 9m) kubelet Back-off restarting failed container app in pod api-7d9f8b6c5d-x2kqp_default(3c1d6f0e-9a2b-4e57-8f1a-6b2d0c9e4a11)
- Init:CrashLoopBackOff
CrashLoopBackOff means the container starts then stops in a loop, and Kubernetes waits longer and longer before restarting it. The cause shows in the logs of the previous instance (kubectl logs <pod> --previous) and in the exit code shown by kubectl describe pod: missing variable, invalid command, overly strict probe or not enough memory.
What this error means
CrashLoopBackOff is not an error in itself, it is a state. The container starts, then ends (crash or normal end of the process). With restartPolicy: Always, the one used by Deployments, the kubelet restarts it, doubling the delay after each failure: 10 s, 20 s, 40 s, up to 5 minutes at most. The delay resets once the container runs for 10 minutes without trouble. The "Back-off restarting failed container" message is the event emitted during that wait. The real cause is always elsewhere: in the logs of the previous attempt, the exit code, or the pod events.
Common causes
- The application crashes on startup: missing environment variable or secret, missing config file, unreachable database, failing migration (code 1).
- Invalid entry command: wrong
command/args, binary not found (code 127), not executable (code 126), wrong architecture (exec format error), or a process that ends immediately (code 0). - Overly strict liveness probe: wrong port or path, or an application slow to start; the kubelet kills it before it is ready.
- Not enough memory: the
resources.limits.memorylimit is exceeded, the container is killed withReason: OOMKilledand code 137. - Permissions or file system:
runAsNonRoot,readOnlyRootFilesystemor a mounted volume prevent the application from writing where it needs to.
Step-by-step fix
-
Read the state, exit code and events
In
kubectl describe pod, look atLast State: Terminated, thenReasonandExit Code, and theEventssection at the bottom. Events sorted by date also show probe failures.kubectl get pods -n default kubectl describe pod api-7d9f8b6c5d-x2kqp -n default kubectl get events -n default --sort-by=.lastTimestamp -
Read the logs of the previous attempt
kubectl logswithout options shows the container that just restarted, often empty.--previousshows the one that crashed. Add-cif the pod has several containers, or the init container name forInit:CrashLoopBackOff.kubectl logs api-7d9f8b6c5d-x2kqp --previous kubectl logs api-7d9f8b6c5d-x2kqp -c app --previous --tail=100 -
Interpret the exit code
1: application error, the answer is in the logs. 126: file not executable. 127: command not found in the image. 137:
SIGKILL, OOMKilled or killed after a failed liveness probe. 143:SIGTERM. 0: the process ended normally, but a Deployment expects a process that stays in the foreground. -
Check the injected configuration
List the Deployment variables and check that the referenced Secrets and ConfigMaps exist with the right keys.
kubectl describe secretshows keys and their size, without revealing the values.kubectl set env deployment/api --list kubectl describe secret app-secrets kubectl describe configmap app-config -
Give startup some time with a startupProbe
If the events show
Liveness probe failed, check the port and path, then add astartupProbe: liveness only starts once the application is up. Here, the application has up to 150 seconds (30 × 5 s) to start.startupProbe: httpGet: path: /healthz port: 8080 periodSeconds: 5 failureThreshold: 30 livenessProbe: httpGet: path: /healthz port: 8080 periodSeconds: 10 failureThreshold: 3 -
Debug inside a copy of the pod
kubectl debugcreates a copy of the pod where theappcontainer command is replaced by a shell: you can inspect files and start the application by hand. Delete the copy afterwards. You can also test the image locally withdocker run --rm -it --entrypoint sh myapp:1.4.2.kubectl debug api-7d9f8b6c5d-x2kqp -it --copy-to=api-debug --container=app -- sh kubectl delete pod api-debug -
Roll back if a deployment broke it
If the problem appeared with the latest version, go back to the previous one while you fix it, then watch the rollout.
kubectl rollout undo deployment/api kubectl rollout status deployment/api
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Have the application validate its configuration on startup, with an explicit message ("DATABASE_URL is missing") rather than an obscure stack trace.
- Test the image locally with the same variables and command as in the manifest before deploying.
- Separate the probes:
startupProbefor startup,readinessProbefor traffic (it does not restart the container),livenessProbeonly to detect a real hang.
Frequently asked questions
How do I see the logs of a pod in CrashLoopBackOff?
With kubectl logs <pod> --previous, which shows the output of the container that just crashed. Without --previous, you see the new attempt, often still empty.
Does deleting the pod fix the problem?
No: the Deployment recreates a pod with the same spec, which will crash the same way. Fix the cause (config, image, probes, resources), then redeploy.
What is the difference between CrashLoopBackOff and ImagePullBackOff?
ImagePullBackOff: Kubernetes cannot download the image (name, tag, private registry). CrashLoopBackOff: the image is there, the container starts, then stops.
Useful tools for this error
More Docker & DevOps errors
- Fix "permission denied while trying to connect to the Docker daemon socket"
- Fix "Bind for 0.0.0.0:8080 failed: port is already allocated" (Docker)
- Fix "no space left on device" (Docker and Linux)
- Fix "exec format error" in Docker (arm64 / amd64)
- Fix "Cannot connect to the Docker daemon. Is the docker daemon running?"
- Fix "connect() failed (111: Connection refused) while connecting to upstream" (Nginx 502)
- Fix "exited with code 137" (OOMKilled) in Docker and Kubernetes