Docker Container Keeps Restarting: Find the Cause

By Sudipta Dhara / November 3, 2026

By ProDevs

Published 2026-11-03

AI disclosure: drafted with AI assistance. Every output below comes from our own run on 3 November 2026.

Who this is for: developers and DevOps engineers · Beginner · You’ll need: a Linux host with Docker, and the name of the container that keeps restarting

Short answer

Run docker inspect on the container and read four fields: State.ExitCode, State.OOMKilled, State.Error and RestartCount. Exit 137 with OOMKilled true means the memory limit killed it. Exit 1 means the app quit on its own, so read docker logs. Exit 127 or 126 means the command could not start. An unhealthy status does not restart a plain Docker container at all.

How we tested: we reproduced five failures with alpine:3.22 containers on a disposable test machine (8 vCPU, 15 GB RAM, ext4, Debian 13, Docker Engine 26.1.5), then read each one with the commands below. This was a test box, not a production server.

Prerequisites

  • Shell access to the Docker host.
  • The container name. docker ps -a lists it even when it is stopped or restarting.
  • Permission to run docker (root or the docker group).

Step 1: See what Docker thinks is happening

docker ps -a --format 'table {{.Names}}\t{{.Status}}'  # tested on Docker Engine v26.1.5

On our test host, 45 seconds after starting all five containers:

NAMES       STATUS
hc          Up 45 seconds (unhealthy)
app1        Exited (1) 41 seconds ago
noexec      Created
badcmd      Created
oom         Restarting (137) 18 seconds ago

The status already narrows it down. Restarting (137) is a loop. Created means the process never started. Exited (1) after a restart policy means Docker gave up.

Step 2: Read the exit code and the OOM flag

docker inspect oom --format \
  'ExitCode={{.State.ExitCode}} OOMKilled={{.State.OOMKilled}} RestartCount={{.RestartCount}} Error={{.State.Error}}'

What each of our containers printed:

Container What we ran ExitCode OOMKilled RestartCount Status
oom --memory 32m --memory-swap 32m, process reads /dev/zero into memory 137 true 9 in 45 s restarting
app1 --restart on-failure:5, app exits 1 1 false 5 exited
badcmd command /app/start.sh, which is not in the image 127 false 0 created
noexec command is a file without the execute bit 126 false 0 created
hc healthcheck against a port nothing listens on 0 false 0 running, unhealthy

Read the table like this:

  • 137 and OOMKilled=true: the kernel killed the process because the container hit its memory limit. Raise --memory, or find why the app grows. Our Docker memory limits lab walks through this case.
  • 137 and OOMKilled=false: something sent SIGKILL, often a docker stop that ran past its timeout. That is a different problem.
  • 1 (or another small number): the app chose to exit. The reason is in its logs (Step 3).
  • 127 or 126: the command could not run. State.Error says why (Step 4).

Step 3: Read the logs from the last run

docker logs --tail 50 app1
FATAL: DATABASE_URL is not set
FATAL: DATABASE_URL is not set

Each restart appends to the same log, so the same line repeats. Fix the config the message names, then docker start the container or recreate it with Compose.

on-failure:5 stopped after five restarts. Docker’s docs say on-failure[:max-retries] limits how many times the daemon tries (Docker docs: restart policies). With always or unless-stopped, there is no limit, and the loop goes on.

Step 4: Check whether the command can even start

For exit 127 and 126, docker logs is empty because nothing ran. The reason is in State.Error:

ExitCode=127 ... Error=failed to create task for container: ... exec: "/app/start.sh": stat /app/start.sh: no such file or directory: unknown
ExitCode=126 ... Error=failed to create task for container: ... exec: "/etc/hostname": permission denied: unknown
  • no such file or directory: the path in CMD, ENTRYPOINT or your Compose command is wrong, or the file was not copied into the image.
  • permission denied: the file exists but is not executable. Add chmod +x in the Dockerfile, or COPY --chmod=755.

These two never entered a loop on our host. They stayed in Created with RestartCount=0, and docker run itself returned the error. Docker’s docs say a restart policy only takes effect after a container starts successfully (restart policies). If you see these in a loop in your setup, the failure is likely inside an entrypoint script that did start, so check docker logs.

Step 5: Check the health status

docker inspect hc --format '{{.State.Health.Status}} streak={{.State.Health.FailingStreak}}'
docker inspect hc --format '{{json (index .State.Health.Log 0)}}'
unhealthy streak=8
{"Start":"...","End":"...","ExitCode":1,"Output":"wget: can't connect to remote host (127.0.0.1): Connection refused\n"}

The health log keeps the check’s own output, which is usually the fastest clue: wrong port, wrong path, or the app is not listening on the address the check uses.

Our hc container was unhealthy with RestartCount=0. Plain Docker marks the container unhealthy after the set number of failed checks (Dockerfile HEALTHCHECK reference), but the restart policy acts on exits, not on health. If you expected an unhealthy container to restart, it will not, unless something else (Swarm, an orchestrator, or your own script) acts on the status.

Step 6: Watch the loop live

docker events --since 10m --filter container=oom --format '{{.Time}} {{.Action}}'

For the oom container we saw repeated oom, die and start events, and a filter on --filter event=oom counted 9 oom events. Docker adds a growing delay between restarts: 100 ms, then doubling, up to one minute (docker run reference). That is why RestartCount climbs fast at first and then slows down.

Verify

After your fix, the container should stay up and the counter should stop moving:

docker inspect oom --format 'Status={{.State.Status}} RestartCount={{.RestartCount}} StartedAt={{.State.StartedAt}}'

Expected output: Status=running, and the same RestartCount when you run it again a few minutes later. If the image has a healthcheck, docker ps should show (healthy).

Troubleshooting

  • Logs are empty and status is Restarting: use docker logs --tail 50 right after a start, or docker events, to catch the short run between restarts.
  • Exit 137 but OOMKilled=false: check for a docker stop or docker kill in your deploy scripts, or a host-level kill.
  • Exit 0 in a loop with always: the main process finished normally. A container lives only as long as its main process, so run the service in the foreground.
  • Compose: docker compose ps and docker compose logs <service> give the same information per service.

Rollback

Stop the loop first so it does not keep spending CPU while you debug:

docker update --restart no oom
docker stop oom

docker update --restart changes the policy on an existing container (restart policies). Set it back to unless-stopped once the fix is in. If a new image caused the loop, redeploy the previous image tag.

FAQ

Why does my Docker container keep restarting?

The main process keeps exiting and the restart policy starts it again. Find out why it exits from docker inspect: exit code, OOMKilled, and State.Error. Then read docker logs for the app’s own message.

What does exit code 137 mean in Docker?

The process got SIGKILL (128 + 9). If OOMKilled is true, the container hit its memory limit. If it is false, something else killed it, often a stop that timed out.

Does Docker restart an unhealthy container?

Not on its own. A failing healthcheck marks the container unhealthy, but restart policies react to the process exiting. Our unhealthy test container kept running with RestartCount 0.

Why does my container stay in the Created state and never start?

The command could not be executed, for example a missing script (exit 127) or a file without execute permission (exit 126). docker inspect --format '{{.State.Error}}' prints the reason.

Sources

Related on ProDevs: Docker memory limits lab, Docker disk space full, Docker VPS production hardening checklist.

Stuck on a container that won’t stay up? See ProDevs services.

Get new ProDevs guides by email

ProDevs Newsletter Form

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

IP Geolocation by DB-IP