Chapter 5 · Lesson 4 of 10
Fix it

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.

By the end you will

Read logs, exit codes and OOMKilled to find why a container restarts in a loop and fix the cause.

Time
15 min
Level
Beginner
Check
4 questions
You need
  • Docker installed on the server
  • The name of the restarting container

Why a container keeps restarting

A container that keeps restarting is stuck in a loop: the process exits, the restart policy starts it again, and the logs from each short run hold the clue you need.
  1. The main process stops with an exit code, such as 1 for an app error or 137 for a kill.
  2. The restart policy tells Docker to start the container again right away.
  3. Each short run writes its last error to the logs before it dies again.
  4. You read the logs to see why the process stops.
  5. docker inspect shows ExitCode, OOMKilled and RestartCount to narrow the cause.
  6. You fix the config, memory limit or bug, and the loop ends.
Static diagram
  1. ContainerexitsDocker daemon
  2. Docker daemonrestartContainer
  3. Containerwrites errorLogs
  4. Youdocker logsLogs
  5. YouinspectDocker daemon
  6. 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#

bash
docker ps -a

Look 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:

bash
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#

bash
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#

bash
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: true if 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:

CodeMeaningUsual cause
0Exited cleanlyA command that finishes, run as a service with restart: always
1General application errorCrash, bad config, failed connection, missing variable
137Killed with SIGKILLOut of memory (check OOMKilled), or docker kill
139Segmentation faultA crash in native code or a bad binary for the platform
143SIGTERMA 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:

bash
docker run --rm -it --env-file .env myapp:latest

Use 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:

bash
docker run --rm -it --entrypoint sh myapp:latest

From 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 environment and env_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#

bash
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#

bash
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 config and 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 --since or the first lines with docker 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 restart does not pick up changed compose settings. Use up -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#

SymptomLikely causeFix
Restarting (1) with a stack traceApplication errorFix config or code from the trace
ExitCode 137, OOMKilled trueMemory limit hitRaise limit, fix leak, add swap
ExitCode 0 and restartsCommand finishes by designRun as a one-off job
ExitCode 127Command not foundFix CMD or ENTRYPOINT, check the image
No logs at allCrashes before outputRun in the foreground with an overridden entrypoint
Loop only after rebootDependency not readyHealth check and depends_on condition

Quick reference#

bash
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>

Next#

Continue with Fix port already in use.

Check what you learned4 questions
1What does Restarting in the STATUS column of docker ps tell you?
2Which command prints the last exit code of a container?
3docker inspect shows ExitCode 137 and OOMKilled true. What is the cause?
4Exit code 143 usually means what?

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

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios