How to configure automatic container restarts using Restart Policies?
In real-world operating conditions, containers can terminate due to code bugs, lack of RAM, or host machine reboots. To keep the application available without manual administrator intervention, Docker provides a mechanism for restart policies. These rules determine whether the Docker daemon should automatically restart a container when it stops.
The policy is set when creating or starting a container using a special flag. Several system behavior options are available depending on your tasks. For example, you can configure a restart only in the event of an unexpected failure with a non-zero exit code.
The main types of restart policies include the following options:
For the on-failure policy, you can additionally limit the number of retries. This is useful to avoid endlessly burdening the system with a non-working application. The limit is specified after a colon following the policy name along with the number of attempts.
Proper configuration of restart policies is a basic requirement for ensuring high availability of applications. It relieves developers and system administrators from the need to constantly monitor container states manually.