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

URL: https://levelrail.com/learn/troubleshooting/fix-a-docker-container-that-keeps-restarting/
Updated: 2026-11-27

`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](https://levelrail.com/learn/glossary/#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](https://levelrail.com/learn/docker/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](https://levelrail.com/learn/deploy-operate/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:

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

```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](https://levelrail.com/learn/docker/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](https://levelrail.com/learn/docker/docker-healthchecks/)).
- OOMKilled true (137): Raise the container's memory limit if it is too low, fix a leak, or add [swap](https://levelrail.com/learn/server-basics/add-swap-space-on-ubuntu/)). See [fix out of memory kills](https://levelrail.com/learn/troubleshooting/fix-out-of-memory-kills-oom/)).
- 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](https://levelrail.com/blog/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

| 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

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

## Related lessons

- [Docker restart policies](https://levelrail.com/learn/docker/docker-restart-policies/)): what each policy does and when to use it.
- [Docker health checks](https://levelrail.com/learn/docker/docker-healthchecks/)): notice a broken container before users do.
- [Read container logs](https://levelrail.com/learn/deploy-operate/read-container-logs/)): filter and follow logs while you debug.
- [Fix out of memory kills](https://levelrail.com/learn/troubleshooting/fix-out-of-memory-kills-oom/)): the full path for exit code 137.
- [Run commands inside a container](https://levelrail.com/learn/docker/run-commands-inside-a-container/)): explore a container that does start.

## Next

Continue with [Fix port already in use](https://levelrail.com/learn/troubleshooting/fix-port-already-in-use/)).

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

- [Docker docs, start containers automatically](https://docs.docker.com/engine/containers/start-containers-automatically/)
