Docker容器 restart策略
2016-07-29 by dongnan
举个栗子
假设 docker
宿主机因某种原因需要重新启动,希望设置容器跟随宿主机自行启动。
docker run -d --restart="always" \
--publish="8500:8500" \
--hostname="consul" --name="consul" consul
注意, --restart="always"
参数。
帮助
默认情况下 docker
宿主机重启后容器是 stop
状态的,通过在创建容器时添加 --restart
参数可以实现容器跟随宿主机(Docker
守护进程)自行重启。
# 输入命令
docker run --help | grep "restart"
--restart=no Restart policy to apply when a container exits
重启策略
docker
提供了其它的容器 restart
策略,例如下面的容器正常关闭的容器不会自行重启。
docker run --name $(hostname) --restart=unless-stopped -p 6000:6000 -tid test:0.3
重启参数
no, 不重新启动这是默认策略。
Do not automatically restart the container when it exits. This is the default.
on-failure[:max-retries]
,设置重新启动最大次数,达到限制则不在尝试重新启动容器。
Restart only if the container exits with a non-zero exit status.
Optionally, limit the number of restart retries the Docker daemon attempts.
always,无论容器的状态如何,始终尝试重新启动容器。
Always restart the container regardless of the exit status.
When you specify always, the Docker daemon will try to restart the container indefinitely.
The container will also always start on daemon startup, regardless of the current state of the container.
unless-stopped,如果容器以前已处于停止状态,那么则不再尝试重新启动容器。
Always restart the container regardless of the exit status,
but do not start it on daemon startup if the container has been put to a stopped state before.