Prefer restart: on-failure:5 in Docker
Whenever I set up an open source project with Docker Compose, I pretty much always see people use restart: always
as the restart policy, which will:
Always restart the container if it stops. If it's manually stopped, it's restarted only when Docker daemon restarts or the container itself is manually restarted.
Using always
can lead to crash loops, which is how I learnt Discord will reset your bot token if it connects and disconnects to their gateway 1000 times in a short period.
The Docker documentation also lists on-failure[:max-retries]
as an option, which I think you should prefer:
Restart the container if it exits due to an error, which manifests as a non-zero exit code. Optionally, limit the number of times the Docker daemon attempts to restart the container using the :max-retries option. The on-failure policy only prompts a restart if the container exits with a failure. It doesn't restart the container if the daemon restarts.
At least for my own uses, if a container crashes, it's not super common for a restart to fix it; usually, it requires some configuration change or similar. There are times though, so I like using on-failure:5
so that it will try a small and finite amount of times before giving up.