A Docker container is restarting in a loop
Read the status, exit code, logs and Compose configuration before disabling the restart policy: the loop is a symptom, the first error lines are the cause.
A restart: unless-stopped policy immediately restarts a failing application. The loop is a symptom; the first lines of error remain the cause to be corrected.
Step-by-step procedure
-
Show all containers
Use docker ps -a to see the status, exit code and restart frequency.
-
Read previous logs
Run docker logs --tail 200 and look for a missing variable, a busy port, a permission error or a failed migration.
-
Inspect the configuration
Check the mounted volumes, variables, healthcheck and memory limits in Compose.
-
Correct then recreate
After fixing, run docker compose up -d and monitor the status without deleting the volumes.
Diagnostic Docker
docker ps -a
docker inspect --format="{{.State.Status}} {{.State.ExitCode}} {{.State.Error}}" NOM
docker logs --tail 200 NOM
docker compose config
Pause the loop and explore the image
docker update --restart=no NOM
docker stop NOM
docker inspect --format '{{.State.OOMKilled}} {{.RestartCount}}' NOM
docker run --rm -it --entrypoint sh IMAGE
Before you start
A loop shows up in docker ps -a as the Restarting state, or as a counter that keeps rising in docker inspect --format '{{.RestartCount}}' followed by the container name. The exit code in brackets guides the diagnosis:
- 0: the main process ends normally, nothing stays in the foreground.
- 1: application error (configuration, missing variable, dependency).
- 126 or 127: command not executable or not found in the image.
- 137: stopped by SIGKILL, often a memory limit or the VPS OOM killer.
Understanding restart policies
no never restarts; on-failure restarts only after a non-zero code; always always restarts, including when Docker starts; unless-stopped does the same unless you stopped the container with docker stop. Docker doubles the delay between restarts, up to one minute, as long as the container does not stay up for 10 seconds.
To read the error calmly, pause the loop with docker update --restart=no then docker stop, each followed by the container name. Put the original policy back once the cause is fixed.
Check that everything works
After docker compose up -d, wait one minute: docker ps must show Up for more than a minute, or (healthy) if a healthcheck exists, and RestartCount must stop rising. Follow the logs with docker compose logs -f --tail 50.
Frequently asked questions
The logs show exec format error, why?
The image was built for another architecture, often ARM (arm64), while the VPS runs x86-64. Rebuild it with docker build --platform linux/amd64 or pick the vendor amd64 tag. An entrypoint script saved with Windows line endings causes no such file or directory instead.
The container does not start with bind: address already in use, how do I fix it?
The published port is already taken on the VPS, often by an Nginx or Apache installed outside Docker. ss -tlnp shows the process holding it. Stop that service or publish another host port, for example 8080:80 under ports.
A database loops with Permission denied on its volume, what should I do?
The mounted folder belongs to a different user than the one in the image. Compare ls -ln on the folder with the container user, often UID 999 for the official PostgreSQL and MySQL images, then fix it with chown -R 999:999 followed by the path. Do not delete the volume to start from scratch without a backup.