← Back to Notes
DockerJuly 2, 2026

How to Keep a Docker Container Running Forever with restart: unless-stopped

Use this for any container that needs to be treated like a real background service, not something you manually babysit.

Steps

  1. In docker-compose.yml, add restart: unless-stopped under the service definition.
  2. Apply it: docker compose down && docker compose up -d.
  3. That's genuinely it, no daemon config, no external process manager needed.

Gotcha

restart: unless-stopped means the container restarts after a crash or a full machine reboot, but not if you manually stopped it yourself (docker compose down or docker stop). If you want it to come back even after a manual stop, the option is always instead, though for most personal projects unless-stopped is the safer default, since it respects an intentional stop.

Also worth knowing: this only restarts the container. If the whole machine reboots, Docker Desktop itself also needs to be set to auto-start, otherwise there's nothing to restart the container in the first place.

Quick reference

services:
  app:
    restart: unless-stopped