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 dockerd command. Using –live-restore lets you to keep your containers running during a Docker upgrade, though networking and user input are interrupted.
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)
Restart policy details
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.
For details you can read more from docker official doc site- https://docs.docker.com/config/containers/start-containers-automatically/