Sobes.tech
Junior — Middle

What are the strategies for restarting containers in Docker and how do they work?

sobes.tech AI

Answer from AI

Docker has several container restart strategies, which are set using the --restart option when starting a container. The main ones are:

  • no — the container does not restart automatically (default value).
  • on-failure[:max-retries] — the container restarts only if it exits with an error (exit code != 0). You can specify a maximum number of retries.
  • always — the container always restarts, regardless of the exit code.
  • unless-stopped — the container always restarts, except when it was stopped manually.

For example, docker run --restart=on-failure:3 will attempt to restart the container up to 3 times on errors.

These strategies help ensure service resilience by automatically recovering containers after failures.