Fix a Docker container that keeps restarting
A container that keeps restarting has a main process that exits, and its restart policy starts it again; docker logs and the exit code from docker inspect show why it exits.
- Updated Nov 27, 2026
- Markdown
Read logs, exit codes and OOMKilled to find why a container restarts in a loop and fix the cause.
- Docker installed on the server
- The name of the restarting container
Why a container keeps restarting
- The main process stops with an exit code, such as 1 for an app error or 137 for a kill.
- The restart policy tells Docker to start the container again right away.
- Each short run writes its last error to the logs before it dies again.
- You read the logs to see why the process stops.
- docker inspect shows ExitCode, OOMKilled and RestartCount to narrow the cause.
- You fix the config, memory limit or bug, and the loop ends.
Static diagram
- ContainerexitsDocker daemon
- Docker daemonrestartContainer
- Containerwrites errorLogs
- Youdocker logsLogs
- YouinspectDocker daemon
- Youfix the causeContainer
docker ps shows Restarting (1) 12 seconds ago in the STATUS column, or the same container shows a tiny uptime every time you look. Something inside the container stops, and Docker starts it again, over and over. The fix is to find out why the process exits. The exit code, the last log lines and the memory flag tell you almost everything.
How it works#
A container lives as long as its main process (PID 1). When that process exits, the container stops. If a restart policy such as always or unless-stopped is set, Docker starts it again, waiting a little longer between attempts. A crash on startup then looks like a loop. Without a restart policy, the same crash would leave the container in Exited status, which is easier to spot.
So the policy is not the bug. It is the thing that makes the bug visible. See Docker restart policies for what each value does.
Step 1: Confirm the loop#
docker ps -aLook at the STATUS column. Restarting (1) 5 seconds ago is a live loop, and the number is the last exit code. Up 3 seconds with a very small uptime that keeps resetting is the same thing caught between attempts. Note the container name.
Stop the churn so you can inspect calmly:
docker stop <container>
docker update --restart=no <container>Setting the restart policy to no is temporary and you will restore it after the fix.
Step 2: Read the logs#
docker logs --tail 100 --timestamps <container>Docker keeps the container’s logs across restarts, so this includes earlier attempts. There is no --previous flag in Docker, that belongs to Kubernetes. The last lines before each exit are the most useful: a stack trace, cannot find module, permission denied, connection refused to a database, address already in use, or a message about a missing environment variable. If the container exits too fast to print anything, look at Step 3. More on reading logs is in read container logs.
Step 3: Check exit code, OOMKilled and restart count#
docker inspect --format 'exit={{.State.ExitCode}} oom={{.State.OOMKilled}} restarts={{.RestartCount}} error={{.State.Error}}' <container>Read the three fields:
ExitCode: how the process ended.OOMKilled:trueif the kernel killed it for exceeding its memory limit.RestartCount: how many times Docker restarted it. A rising count confirms the loop.
Common exit codes:
| Code | Meaning | Usual cause |
|---|---|---|
| 0 | Exited cleanly | A command that finishes, run as a service with restart: always |
| 1 | General application error | Crash, bad config, failed connection, missing variable |
| 137 | Killed with SIGKILL | Out of memory (check OOMKilled), or docker kill |
| 139 | Segmentation fault | A crash in native code or a bad binary for the platform |
| 143 | SIGTERM | A stop request, for example docker stop, or an orchestrator |
Code 126 and 127 mean the command could not be run or was not found, which points at a wrong CMD or ENTRYPOINT.
Step 4: Run it in the foreground#
If the logs are empty, run the same image once without a restart policy and watch it:
docker run --rm -it --env-file .env myapp:latestUse the same environment, mounts and command as the real container. Errors that were hidden become visible on your screen. To explore the image without starting the app, override the entrypoint:
docker run --rm -it --entrypoint sh myapp:latestFrom that shell, check that files and binaries exist and the user can read them.
Step 5: Fix the cause#
Match what you found to a fix:
- Application error (exit 1): Fix the config or the code the log points at. Missing environment variable, wrong database host, or a migration that fails. In Compose, check
environmentandenv_file, and see Docker Compose environment variables. - Dependency not ready: The app starts before its database is accepting connections and exits. Use a health check with
depends_on: condition: service_healthy, or make the app retry. See Docker health checks. - OOMKilled true (137): Raise the container’s memory limit if it is too low, fix a leak, or add swap. See fix out of memory kills.
- Segfault (139): Often an image built for another CPU architecture, or a native library mismatch. Rebuild or pull the correct platform.
- Exits with 0: The command finishes by design. Run it as a one-off job, or use a command that stays in the foreground. A process that daemonizes itself also looks like an exit, since PID 1 returns.
Step 6: Restore the policy and restart#
docker update --restart=unless-stopped <container>
docker start <container>With Compose, edit the file and run docker compose up -d, which recreates the container with the new settings.
Check it worked#
docker ps
docker inspect --format 'restarts={{.RestartCount}} status={{.State.Status}}' <container>The status should read Up with an uptime that keeps growing. After a few minutes RestartCount should not have moved. Also check the app answers, for example with curl -I https://app.example.com. The blog post Docker container restarting in a loop walks through a real case.
Prevent it#
- Keep a restart policy, but also alert when a container restarts more than a few times.
- Add a health check so a running but broken container is noticed.
- Test config changes with
docker compose configand a foreground run before relying on the policy.
Edge cases and gotchas#
- Fast loops hide logs: With
--tail, a very quick loop can bury the first error. Use--sinceor the first lines withdocker logs <container> | head. - Log rotation: If you limit log size, old attempts may be gone. That is another reason to read logs early.
- Healthcheck restarts: Docker alone does not restart on an unhealthy status. Something else, such as an autoheal tool, may be doing it.
- Host reboots: After a reboot every container with a policy starts together, and a dependency may lose the race.
- Compose recreate:
docker compose restartdoes not pick up changed compose settings. Useup -d. - Exit code is the last one: After many loops, the stored code may differ from the first failure. Check the earliest logs too.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
| Restarting (1) with a stack trace | Application error | Fix config or code from the trace |
| ExitCode 137, OOMKilled true | Memory limit hit | Raise limit, fix leak, add swap |
| ExitCode 0 and restarts | Command finishes by design | Run as a one-off job |
| ExitCode 127 | Command not found | Fix CMD or ENTRYPOINT, check the image |
| No logs at all | Crashes before output | Run in the foreground with an overridden entrypoint |
| Loop only after reboot | Dependency not ready | Health check and depends_on condition |
Quick reference#
docker ps -a
docker logs --tail 100 --timestamps <container>
docker inspect --format 'exit={{.State.ExitCode}} oom={{.State.OOMKilled}} restarts={{.RestartCount}}' <container>
docker update --restart=no <container>
docker run --rm -it --entrypoint sh <image>Related lessons#
- Docker restart policies: what each policy does and when to use it.
- Docker health checks: notice a broken container before users do.
- Read container logs: filter and follow logs while you debug.
- Fix out of memory kills: the full path for exit code 137.
- Run commands inside a container: explore a container that does start.
Next#
Continue with Fix port already in use.
Check what you learned
Frequently asked questions
Why does my Docker container keep restarting?
Its main process exits, and the restart policy starts it again. The cause is usually a crash on startup, a missing environment variable or config, a failed dependency, or the process being killed for using too much memory.
What does exit code 137 mean in Docker?
Exit code 137 means the process was killed with SIGKILL (128 plus 9). The most common reason is the kernel out of memory killer, which docker inspect confirms with OOMKilled true. A manual docker kill also gives 137.
How do I see the logs of a container that keeps restarting?
Run docker logs with the container name. Docker keeps the logs across restarts, so you see output from previous attempts too. Add --tail 100 and --timestamps to focus on the latest runs.
How do I stop a restart loop?
Run docker update --restart=no on the container or docker stop it, then fix the cause. Stopping first gives you a stable container to inspect.
Sources
Keep going: guides and tutorials
- Docker Container Restarting in a Loop: 5 Causes, Fixed Five real reasons a Docker container keeps restarting, each reproduced with its exit code, the command that finds it and the fix. Then the same crash on Levelrail.
- Our Health Check Failed and the Deploy Went Live Anyway A postmortem from our own dogfood run: a failed readiness probe still received traffic. What broke, why two correct parts added up to a bug, and the fix.
- Roll Back a Failed Docker Deploy Without Downtime Ship a broken release on purpose, watch your users stay on the old version, then roll back with one command. Real commands, real output, self-hosted.