Your Docker container keeps restarting. Here's how to find out why

A container in a restart loop can look healthy at a glance. How to spot it, read the exit code, find the real error, and fix the most common causes.

AskMyServers team · · 3 min read

A container stuck in a restart loop is easy to miss. Run docker ps at the right moment and it says "Up", same as everything else. Look again ten seconds later and it says "Up 3 seconds", then "Restarting". Meanwhile whatever it was supposed to be doing isn't getting done.

Here's how we track these down.

1. Confirm it's looping

docker ps -a --format 'table {{.Names}}\t{{.Status}}'
docker inspect -f '{{.RestartCount}} restarts, last exit {{.State.ExitCode}}' my-container

A restart count that keeps going up is the giveaway. Note the exit code; it tells you a lot.

2. Read the exit code

Exit codeUsually means
0The process finished. It isn't meant to run forever, or the command is wrong
1The application hit an error and quit
125–127Docker couldn't run the command (typo, missing file, not executable)
137Killed, very often by the out-of-memory killer
139Segmentation fault
143Stopped by SIGTERM (normal during shutdowns)

Exit 0 surprises people. If your container runs a script that starts a background process and then exits, Docker sees the main process end and restarts it, forever.

3. Look at the logs from the last run

docker logs --tail 100 my-container
docker logs --since 10m my-container

The error you want is usually in the last few lines before it died. If the container restarts too quickly to catch the output, the logs from previous runs are still there, so --since is your friend.

4. Check for out-of-memory kills

Exit 137 plus no obvious error in the logs usually means the kernel killed it:

docker inspect -f 'OOMKilled={{.State.OOMKilled}}' my-container
sudo dmesg -T | grep -i -E 'killed process|out of memory' | tail

If it was OOM-killed, either give it more memory (--memory / mem_limit) or find out why it uses so much. A limit that was fine last month can become too small after an upgrade.

5. The usual causes

From experience, most restart loops come down to a short list:

  • A missing or wrong environment variable, such as a database URL or API key.
  • A dependency that isn't ready yet: the app starts before the database accepts connections and gives up. Add a retry or a proper health check.
  • A config file that isn't where the app expects it, often because a volume isn't mounted.
  • Not enough memory, see above.
  • A health check that fails, so the orchestrator keeps replacing a perfectly good container. Check the health check command itself.
  • A port already in use on the host.

6. Get a shell in a broken image

If the container dies too quickly to inspect, start the same image with a shell instead of its normal command:

docker run --rm -it --entrypoint sh my-image

Then run the real command by hand and watch what happens.

Catching it next time

The annoying thing about restart loops is how long they can go unnoticed. AskMyServers reads each container's restart count from the agent, so the health page lists any container that has restarted repeatedly, and you can ask your assistant "which container keeps restarting?" instead of checking each server.

Monitor your containers

Your Docker container keeps restarting. Here's how to find out why · AskMyServers