Start containers automatically
Docker provides restart policies to control whether your containers start automatically when they exit, or when Docker restarts. Restart policies start linked containers in the correct order. Docker recommends that you use restart policies, and avoid using process managers to start containers.
Restart policies are different from the
--live-restore flag of the
--live-restore lets you to keep your containers running during
a Docker upgrade, though networking and user input are interrupted.
To configure the restart policy for a container, use the
when using the
docker run command. The value of the
--restart flag can be
any of the following:
|Don't automatically restart the container. (Default)|
|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 |
|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)|
|Similar to |
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
$ docker update --restart unless-stopped redis
The following command ensures all running containers restart.
$ docker update --restart unless-stopped $(docker ps -q)
Keep the following in mind when using restart policies:
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 10 seconds and Docker has started monitoring it. This prevents a container which doesn't start at all from going into a restart loop.
If you manually stop a container, the restart policy is ignored until the Docker daemon restarts or the container is manually restarted. This prevents a restart loop.
Restart policies only apply to containers. To configure restart policies for Swarm services, see flags related to service restart.
If restart policies don't suit your needs, such as when processes outside Docker depend on Docker containers, you can use a process manager such as systemdopen_in_new or supervisoropen_in_new instead.
Don't combine Docker restart policies with host-level process managers, as this creates conflicts.
To use a process manager, configure it to start your container or service using
docker start or
docker service command you would normally use to
start the container manually. Consult the documentation for the specific
process manager for more details.
Process managers can also run within the container to check whether a process is running and starts/restart it if not.
These aren't Docker-aware, and only monitor operating system processes within the container. Docker doesn't recommend this approach, because it's platform-dependent and may differ between versions of a given Linux distribution.