Merge pull request #18228 from dvdksn/engine-on-failure-daemon-restart

clarify restart-policy on-failure with daemon restart
This commit is contained in:
David Karlsson 2023-09-19 16:33:30 +02:00 committed by GitHub
commit 6c31b96697
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 31 deletions

View File

@ -3,20 +3,20 @@ description: How to start containers automatically
keywords: containers, restart, policies, automation, administration keywords: containers, restart, policies, automation, administration
title: Start containers automatically title: Start containers automatically
aliases: aliases:
- /engine/articles/host_integration/ - /engine/articles/host_integration/
- /engine/admin/host_integration/ - /engine/admin/host_integration/
- /engine/admin/start-containers-automatically/ - /engine/admin/start-containers-automatically/
--- ---
Docker provides [restart policies](../../engine/reference/run.md#restart-policies---restart) Docker provides [restart policies](../../engine/reference/run.md#restart-policies---restart)
to control whether your containers start automatically when they exit, or when to control whether your containers start automatically when they exit, or when
Docker restarts. Restart policies ensure that linked containers are started in Docker restarts. Restart policies start linked containers in the correct order.
the correct order. Docker recommends that you use restart policies, and avoid Docker recommends that you use restart policies, and avoid using process
using process managers to start containers. managers to start containers.
Restart policies are different from the `--live-restore` flag of the `dockerd` Restart policies are different from the `--live-restore` flag of the `dockerd`
command. Using `--live-restore` allows you to keep your containers running command. Using `--live-restore` lets you to keep your containers running during
during a Docker upgrade, though networking and user input are interrupted. a Docker upgrade, though networking and user input are interrupted.
## Use a restart policy ## Use a restart policy
@ -24,27 +24,28 @@ To configure the restart policy for a container, use the `--restart` flag
when using the `docker run` command. The value of the `--restart` flag can be when using the `docker run` command. The value of the `--restart` flag can be
any of the following: any of the following:
| Flag | Description | | Flag | Description |
|:-----------------|:------------------------------------------------------------------------------------------------| | :------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `no` | Do not automatically restart the container. (the default) | | `no` | Don't automatically restart the container. (Default) |
| `on-failure[:max-retries]` | 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. | | `on-failure[:max-retries]` | 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. |
| `always` | Always restart the container if it stops. If it is manually stopped, it is restarted only when Docker daemon restarts or the container itself is manually restarted. (See the second bullet listed in [restart policy details](#restart-policy-details)) | | `always` | 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. (See the second bullet listed in [restart policy details](#restart-policy-details)) |
| `unless-stopped` | Similar to `always`, except that when the container is stopped (manually or otherwise), it is not restarted even after Docker daemon restarts. | | `unless-stopped` | Similar to `always`, except that when the container is stopped (manually or otherwise), it isn't restarted even after Docker daemon restarts. |
The following example starts a Redis container and configures it to always The following command starts a Redis container and configures it to always
restart unless it is explicitly stopped or Docker is restarted. restart, unless the container is explicitly stopped, or the daemon restarts.
```console ```console
$ docker run -d --restart unless-stopped redis $ docker run -d --restart unless-stopped redis
``` ```
This command changes the restart policy for an already running container named `redis`. The following command changes the restart policy for an already running
container named `redis`.
```console ```console
$ docker update --restart unless-stopped redis $ docker update --restart unless-stopped redis
``` ```
And this command will ensure all currently running containers will be restarted unless stopped. The following command ensures all running containers restart.
```console ```console
$ docker update --restart unless-stopped $(docker ps -q) $ docker update --restart unless-stopped $(docker ps -q)
@ -57,17 +58,16 @@ Keep the following in mind when using restart policies:
- A restart policy only takes effect after a container starts successfully. In - A restart policy only takes effect after a container starts successfully. In
this case, starting successfully means that the container is up for at least this case, starting successfully means that the container is up for at least
10 seconds and Docker has started monitoring it. This prevents a container 10 seconds and Docker has started monitoring it. This prevents a container
which does not start at all from going into a restart loop. which doesn't start at all from going into a restart loop.
- If you manually stop a container, its restart policy is ignored until the - If you manually stop a container, the restart policy is ignored until the
Docker daemon restarts or the container is manually restarted. This is another Docker daemon restarts or the container is manually restarted. This prevents
attempt to prevent a restart loop. a restart loop.
- Restart policies only apply to _containers_. Restart policies for swarm - Restart policies only apply to containers. To configure restart policies for
services are configured differently. See the Swarm services, see
[flags related to service restart](../../engine/reference/commandline/service_create.md). [flags related to service restart](../../engine/reference/commandline/service_create.md).
## Use a process manager ## Use a process manager
If restart policies don't suit your needs, such as when processes outside If restart policies don't suit your needs, such as when processes outside
@ -78,8 +78,8 @@ Docker depend on Docker containers, you can use a process manager such as
> **Warning** > **Warning**
> >
> Do not try to combine Docker restart policies with host-level process managers, > Don't combine Docker restart policies with host-level process managers,
> because this creates conflicts. > as this creates conflicts.
{ .warning } { .warning }
To use a process manager, configure it to start your container or service using To use a process manager, configure it to start your container or service using
@ -94,8 +94,8 @@ running and starts/restart it if not.
> **Warning** > **Warning**
> >
> These are not Docker-aware and just monitor operating system processes within > These aren't Docker-aware, and only monitor operating system processes within
> the container. Docker does not recommend this approach, because it is > the container. Docker doesn't recommend this approach, because it's
> platform-dependent and even differs within different versions of a given Linux > platform-dependent and may differ between versions of a given Linux
> distribution. > distribution.
{ .warning } { .warning }