Your site goes down at 2 a.m. because a container crashed, and nothing brings it back until you wake up and type docker start by hand. That is the exact problem a restart policy solves. Learning how to auto-restart crashed Docker containers on your VPS takes about ten minutes, and it turns a middle-of-the-night outage into a short blip nobody notices.
Why Docker containers crash on a VPS
Containers on a small VPS crash for boring reasons. The most common one is memory. A WordPress container, a database, and a cache can fit fine on a 2 GB VPS most of the day, then a traffic spike or a backup job pushes memory use over the limit and the kernel kills a process.
Other usual causes are a failed health check in the app itself, a database that restarts and drops connections, and a host reboot after a kernel update. A restart policy does not fix a broken app. It buys you uptime while you find the real cause.
Before you set anything to restart forever, check whether the container is short on memory. Run docker stats --no-stream and compare the memory column with the limit you set, if any. If a container keeps dying from out-of-memory kills, a restart policy will only restart it into the same kill.
How to auto-restart crashed Docker containers with a restart policy
A docker restart policy tells the Docker daemon what to do when a container stops. You set it with the --restart flag on docker run, or with the restart: key in a Compose file. There are four options.
| Policy | What it does | Use it for |
|---|---|---|
no |
Never restarts the container | One-off jobs, manual tests, containers you run by hand |
on-failure |
Restarts only when the container exits with a non-zero code | Workers and scripts that should retry after an error |
always |
Restarts the container no matter how it stopped, including after a host reboot | Long-running services: web servers, databases, reverse proxies |
unless-stopped |
Same as always, except it stays stopped if you stopped it yourself |
Services you sometimes stop by hand for maintenance |
For most services on a VPS, the restart policy unless-stopped is the right default. It survives a host reboot, it brings a crashed container back, and it respects your decision when you run docker stop on purpose. always is almost the same, but it will restart a container you stopped by hand the next time the daemon restarts.
on-failure fits a different job. A backup script or a queue worker that exits with an error code should try again, but a container that stopped cleanly with exit code 0 should stay stopped. Add :5 to cap the retries, as in on-failure:5. Note that on-failure does not restart a container after a host reboot, because a reboot is not a failure exit.
Start a new container with a policy like this:
docker run -d --name nginx --restart unless-stopped -p 80:80 nginx:alpine
Check the policy Docker recorded for a container with:
docker inspect nginx --format '{{.HostConfig.RestartPolicy.Name}}'
Set a restart policy on a container that already runs
You do not need to delete a running container to add a policy. The docker update command changes the restart policy on the spot, and it takes effect the next time the container stops. Run it, then restart the container once so the new policy is active from a clean start:
docker update --restart unless-stopped nginx docker restart nginx
Auto-restart in Docker Compose
Most VPS stacks run under Compose, and the setting belongs in the Compose file so it survives a redeploy. Add one line per service:
services:
web:
image: nginx:alpine
restart: unless-stopped
ports:
- "80:80"
db:
image: mariadb:11
restart: unless-stopped
environment:
MARIADB_ROOT_PASSWORD: change-me
volumes:
db-data:
After editing the file, apply it with docker compose up -d. Compose recreates only the services whose config changed, so the rest keep running. If you prefer the strictest option, docker compose restart always works the same way: write restart: always under the service instead. Use it only for services that must come back no matter what, such as a reverse proxy in front of everything else.
One detail catches people out: depends_on only controls start order, not readiness. Your web container can start before the database accepts connections, crash on a refused connection, and then get restarted by the policy. A healthcheck on the database plus condition: service_healthy on the web service is the cleaner fix, and it is covered in our guide to Docker Compose healthchecks that stop containers restarting in a loop.
Stop the restart loop before it starts
A restart policy with no healthcheck can hide a broken app. The container starts, crashes after three seconds, restarts, crashes again, and Docker keeps the loop going. The service looks alive in docker ps because a container exists, but it never stays up long enough to serve a request.
If a docker container keeps restarting, spot the loop with the restart count and the status column:
docker ps -a --format '{{.Names}} {{.Status}}'
A status like Restarting (1) 5 seconds ago that never changes to Up means the loop is running. Check the logs next, fix the cause, and only then let the policy do its job. For a full walkthrough of writing healthchecks that catch this early, see how to set up health checks for Docker containers on a VPS. Pair the policy with a healthcheck and a memory limit, so Docker knows the difference between a running container and a working one, and a looping container cannot eat all the RAM while it retries.
Check logs and status after a crash
A restart policy is only half the job. The other half is knowing why the container stopped, or the same crash will repeat. Start with the status and the recent logs:
docker ps -a
docker logs --tail 100 nginx
docker inspect nginx --format '{{.State.Status}} {{.State.ExitCode}}'
The container exit code tells you where to look. Code 0 means the process stopped cleanly, which usually points to a config that tells the app to exit. Code 137 means the container was killed, and on a small VPS that almost always means out of memory. Code 1 is a plain app error, and the logs above it usually name the missing file, bad password, or refused connection.
Logs grow fast when a container restarts in a loop, and on a small disk they can fill the VPS. Set a log size cap in your Compose file or daemon config. Our guide to Docker log management that stops logs filling the disk shows the exact settings.
For the bigger picture, check the host as well. Run dmesg | grep -i oom to see if the kernel killed a process, and free -h to see how much memory is left. If the kernel killed your database twice in one day, fix the memory pressure first. No restart policy will save that service.
When Docker alone is not enough
A restart policy handles crashes inside Docker. It does not handle everything else. If the Docker daemon itself fails, if the host runs out of disk, or if the container is up but serving 500 errors, the policy sees nothing wrong and does nothing.
For the daemon layer, check systemctl is-enabled docker first. Most distros already ship Docker with a systemd unit that starts it on boot and restarts it on failure. The same systemd timer pattern from running scheduled tasks with systemd timers instead of cron also works for a periodic health script that restarts a stack when it stops answering.
For the “up but broken” case, add outside monitoring. An uptime check that hits your site every minute and alerts you when it stops answering catches what a restart policy cannot. Uptime Kuma runs in a container on the same VPS and sends alerts to Telegram, email, or a webhook, and it costs nothing to self-host.
Common mistakes to avoid
- Using
alwayson a one-off container, such as a migration or a backup job, so it reruns every time the host reboots. - Setting a restart policy and never reading the logs, so a crash loop runs for weeks unnoticed.
- Skipping the healthcheck, so Docker restarts a container that starts but never becomes ready.
- Forgetting that
on-failuredoes not survive a host reboot, then wondering why the service stayed down after an update. - Restarting a container by hand during maintenance while
alwaysis set, then finding it back up before the work is done.
Pick unless-stopped for services, on-failure with a retry cap for jobs, and no for anything you run once by hand. Add a healthcheck, cap the logs, and set up an outside uptime check. That combination covers the crash, the loop, and the silent failure.