Sub topic 14
Troubleshooting a Restart Loop
This chapter assumes you can run containers with docker run, read docker ps, and know that exit code 0 means success and non-zero means failure. It also assumes you know the four restart policies: no, on-failure, always and unless-stopped.
What a restart loop looks like
A restart loop happens when a container exits, its restart policy tells Docker to start it again, and it exits again. With always or unless-stopped this can go on forever. With on-failure:3 it ends after three retries.
Docker waits between restarts, and the delay grows after each failed attempt. So in docker ps you will usually see a status like Restarting (1) 5 seconds ago. The number in parentheses is the last exit code.
Step 1: Confirm the loop and read the exit code
Reuse the inspect command from earlier in this tutorial. Run it against the container that is misbehaving:
docker inspect -f '{{.HostConfig.RestartPolicy.Name}} {{.RestartCount}} {{.State.Status}}' flakyA restart count that keeps rising confirms the loop. To see the exit code and whether the kernel killed the process for using too much memory, add two more fields:
docker inspect -f '{{.State.ExitCode}} {{.State.OOMKilled}}' flakyStep 2: Read the logs
Logs survive between restarts, so you can read the output of earlier attempts:
docker logs --tail 50 flakyMost loops show a clear error here, such as a missing environment variable, a refused database connection or a bad config file.
Step 3: Stop the loop and reproduce it
While the policy is active, the container keeps restarting under you. Switch the policy off so it stays stopped, then run the same image in the foreground:
docker update --restart no flaky
docker stop flaky
docker run --rm -it alpine sh -c 'exit 1'; echo "exit code: $?"Replace the image and command in the last line with the ones your container uses. Running it by hand lets you watch the failure directly and try fixes quickly.
Common causes
| Exit code | Usual meaning | What to check |
|---|---|---|
| 1 | The application reported an error | Logs, configuration, missing environment variables |
| 125 | Docker itself failed to run the container | The `docker run` options |
| 126 | The command cannot be executed | File permissions, wrong architecture |
| 127 | The command was not found | `ENTRYPOINT` or `CMD` path, missing binary |
| 137 | Killed with SIGKILL, often out of memory | `OOMKilled` field, memory limit |
| 0 | The process finished normally | A short-lived command under `always` |
The last row surprises people. A container whose main process ends successfully will still be restarted by always or unless-stopped. Such a task belongs under no or on-failure, or should be a long-running process.
Other frequent causes are a dependency that is not ready yet, such as a database that is still starting, and a main process that detaches into the background so the container sees it exit.
Step 4: Fix and recreate
Fix the cause in the image, command, environment or limits. Then restore the policy you want and start the container again:
docker update --restart on-failure:3 flaky
docker start flakyIf the fix changed the image or the environment, you must recreate the container with docker run, because docker update only changes policy and resource limits.
Exercise
A container with --restart always shows Restarting (0) 3 seconds ago in docker ps. The application has no bug. What is the likely cause, and what policy fits better?
Show answer
Exit code 0 means the main process finished successfully, and always restarts it anyway. The workload is a one-off task, so use no, or on-failure if it should retry only after real failures.
Exercise
Which commands stop a looping container from restarting so you can debug it?
Show answer
Run docker update --restart no <name> to remove the policy, then docker stop <name>. Afterward, run the same image by hand with docker run --rm -it to see the failure.