How to enable auto restart of a docker container on system reboot ?
How to enable auto restart of a docker container on system reboot ?
https://amalgjose.com/2021/02/12/how-to-enable-auto-restart-of-a-docker-container-on-system-reboot/#:~:text=How%20to%20enable%20auto%20restart%20of%20a%20docker,Ensure%20the%20docker%20container%20has%20restart%20policy%20configured.
启动docker服务
设置docker容器重启属性
If we reboot the machine, all the services in the machine will get stopped and during the restart, only the services that are configured with auto-restart will only start. To start all the docker containers on system reboot, we need to ensure the following things.
1. Ensure docker daemon restarts on system reboot.
The below command works on CentOS/ RHEL and Ubuntu.
sudo systemctl enable docker.service
2. Ensure the docker container has restart policy configured.
docker update --restart=always [container id or container name]
https://zhuanlan.zhihu.com/p/383382470
第三个问题需要做镜像的时候设置CMD 或者 ENTRYPOINT
要让docker容器内的服务能在开机加电后自动启动,要解决三个问题:
1.docker服务能随OS启动而启动
2.docker容器能随docker服务启动而启动
3.docker容器内的服务能随docker容器启动而启动
How to Use Docker Restart Policies to Keep Containers Running
重启策略针对monitoring docker instance
失败重启可以设置次数
There are currently four different restart policies:
no
– This policy will never automatically start a container. This is the default policy for all containers created withdocker run
.always
– Docker will ensure the container is always running. If the container stops, it will be immediately restarted. You can still manually stop the container withdocker stop
but Docker will bring it back up next time the daemon restarts.on-failure
– The container will get restarted if it stops because of an error. Docker won’t bring the container up after the daemon restarts.unless-stopped
– This functions similarly toalways
. The difference is that Docker won’t ever restart the container if it has been manually stopped.
Restart Loops
Docker includes a couple of safeguards against perpetual restart loops. The first is a mandatory time delay before restart policies activate. Docker won’t begin monitoring restarts until a container has been running for at least 10 seconds. This prevents a failed container from continually restarting.
The other specialist behaviour concerns the
docker stop
command. Docker will always respect use ofdocker stop
, so the container won’t immediately restart after you run the command. If you actually want to restart the container, usedocker restart
instead.Limiting Restart Retries
The
on-failure
restart policy lets you specify how many retries should be attempted. Docker will give up and leave the container in a stopped state if it fails to start multiple times in succession.docker run httpd:latest --restart on-failure:5In this example, Docker will try to restart the container five times after a failure (non-zero exit code). If the container fails to start on the fifth attempt, no more retries will be attempted. This option is useful for containers where a persistent starting error is unlikely to be resolved without manual intervention.
Start containers automatically
https://docs.docker.com/config/containers/start-containers-automatically/
官网的参数解释。
always会在docker服务启动后, 启动容器
包括用户手动停止的容器
如果用户希望容器重启, 则用 unless-stopped
To configure the restart policy for a container, use the
--restart
flag when using thedocker run
command. The value of the--restart
flag can be any of the following:
Flag Description no
Do not automatically restart the container. (the 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.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) unless-stopped
Similar to always
, except that when the container is stopped (manually or otherwise), it is not restarted even after Docker daemon restarts.The following example starts a Redis container and configures it to always restart unless it is explicitly stopped or Docker is restarted.
docker run -d --restart unless-stopped redis
This command changes the restart policy for an already running container named
redis
.docker update --restart unless-stopped redis
And this command will ensure all currently running containers will be restarted unless stopped.
docker update --restart unless-stopped $(docker ps -q)
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 upstart, systemd, or supervisor instead.