← All articles

Docker container keeps restarting — how to debug

A restart loop usually means the main process exits immediately. Check docker logs, inspect exit code, verify env vars and volumes, and fix the crash—not the restart policy first.

If a Docker container keeps restarting, the process inside is crashing—not “Docker being flaky.” Read logs and exit codes before changing restart policies or rebuilding images.

Debug steps

  1. Logs

    docker ps -a
    docker logs CONTAINER_NAME --tail 100
  2. Exit code

    docker inspect CONTAINER_NAME --format '{{.State.Status}} exit={{.State.ExitCode}} error={{.State.Error}}'

    Exit 137 often means OOM killed. 1 is generic app failure.

  3. Run interactively (bypass restart loop)

    docker run -it --rm --entrypoint /bin/sh IMAGE

    Or override command in compose temporarily to sleep infinity and exec in.

  4. Check compose / run config

    • Missing DATABASE_URL, wrong volume mount path
    • Port already in use on host
    • Healthcheck too aggressive before app is ready
  5. Resources

    docker stats --no-stream
    dmesg | tail   # OOM killer messages

Common causes

ClueFix
”connection refused” to DBStart DB first, fix network alias
”permission denied” on volumeUID/GID mismatch, fix ownership
Works locally, fails on VPSWrong arch image, missing .env on server
OOM 137Increase memory limit or fix leak

Production caution

Do not docker system prune -a on a live box to “fix” things. Do not remove volumes without knowing their names.

Get help without YOLO commands

Ohuriya AI is an AI DevOps Copilot—you can paste docker logs output and approve diagnostic commands one at a time. Connect your VPS · disk full fixes

Quick answers

Why does my Docker container keep restarting?

The container entrypoint exits with an error—bad config, missing env var, port conflict, or failed healthcheck. Docker restarts it because of --restart unless-stopped or compose restart policy.

What command shows why a container crashed?

docker logs CONTAINER --tail 100 and docker inspect CONTAINER --format '{{.State.ExitCode}} {{.State.Error}}'. Run docker events in another terminal while reproducing.

Should I change restart policy to fix a loop?

No— that hides the crash. Fix the exit reason first. Temporarily docker update --restart=no only to stop the noise while debugging.