iTranslated by AI
The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐳
Summary of Docker Compose Restart Policies
I had a vague understanding that the --restart setting, defined in a Docker Compose file or specified via docker run, simply determined whether a container would automatically start, and I felt somewhat unclear about the specifics.
I used to set it to always or sometimes unless-stopped without fully understanding why.
Since I couldn't find consolidated information, I've summarized it here based on the restart policies in the official Docker documentation, along with information from the internet and books.
I haven't performed verification tests for every single case, so I would appreciate it if you could point out any errors.

Assumed use cases for each setting.
no
- A policy that performs no automatic restarts at all.
- One-off disposable tasks or batch processes, such as database backup scripts.
- Development/debugging: Checking logs or fixing code when a container crashes with an error.
on-failure
- Used when you only want to automatically recover if the application crashes.
- If the container exits normally, it is considered an intended stop and should not be restarted automatically.
- You can also specify the number of restart attempts upon failure.
always
- Services that are expected to be running at all times.
- Used when you want to recover the container as much as possible, even if it stops for some reason.
unless-stopped
- Always attempts to restart unless manually stopped.
- General services (Web servers, databases, APIs, etc.).
- Expected to be running at all times, with automatic recovery if the container goes down due to some failure.
- For example, if you manually stop the service for temporary maintenance, you want it to remain stopped (with
always, it would restart even after a manual stop).
I hope this is helpful for your reference.
Discussion