Docker·57 questions

How to configure automatic container restarts using Restart Policies?

Answer

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:

no — automatic restart is disabled by default for all new containers.
always — the daemon will always restart the container regardless of the exit code, including manual stops.
unless-stopped — similar to always, but the container is not restarted if it was explicitly stopped by the user before the daemon restart.
on-failure — the restart occurs only when the application terminates with an error, returning a non-zero status code.

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.

Was this answer helpful?

More questions in this topic

Related questions from other topics