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 any of the following:
| Flag | Description |
|---|---|
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. 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'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) |
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 command starts a Redis container and configures it to always restart, unless the container is explicitly stopped, or the daemon restarts.
$ docker run -d --restart unless-stopped redis
The following command changes the restart policy for an already running container named redis.
$ docker update --restart unless-stopped redis
The following command ensures all running containers restart.
$ docker update --restart unless-stopped $(docker ps -q)