docker重启参数--restart=always的作用
--restart=always参数能够使我们在重启docker时,自动启动相关容器。
Docker容器的重启策略如下:
no,默认策略,在容器退出时不重启容器
on-failure,在容器非正常退出时(退出状态非0),才会重启容器
on-failure:3,在容器非正常退出时重启容器,最多重启3次
always,在容器退出时总是重启容器
unless-stopped,在容器退出时总是重启容器,但是不考虑在Docker守护进程启动时就已经停止了的容器
————————————————
版权声明:本文为CSDN博主「易生一世」的原创文章,遵循CC 4.0 BY 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/taiyangdao/article/details/73076019
首先我们先看下正常启动容器不加参数的情况重启docker会出现什么情况
一、启动时候添加参数
1、不加–restart=always参数启动容器
[root@localhost /]# docker run -d tomcat
1a7a3b5112fd310dc70903df479203a81d7d64f7c19ea0aca8fff8ece10e04a4
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a7a3b5112fd tomcat "catalina.sh run" 29 seconds ago Up 28 seconds 8080/tcp friendly_clarke
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a7a3b5112fd tomcat "catalina.sh run" 32 seconds ago Up 31 seconds 8080/tcp friendly_clarke
2、重启docker,查看状态,可以看到,重启docker之后,容器也停止了运行,这在生产环境上是非常危险的。
[root@localhost /]# systemctl restart docker
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a7a3b5112fd tomcat "catalina.sh run" About a minute ago Exited (143) 5 seconds ago friendly_clarke
[root@localhost /]#
3、下面我们使用-restart=always参数启动容器
[root@localhost /]# docker run -d --restart always tomcat
c5557254c6d90b1ba8f9a93ff66e6a28f172e92a56cf945af533327f38152fbc
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5557254c6d9 tomcat "catalina.sh run" 4 seconds ago Up 3 seconds 8080/tcp fervent_goldstine
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5557254c6d9 tomcat "catalina.sh run" 8 seconds ago Up 7 seconds 8080/tcp fervent_goldstine
1a7a3b5112fd tomcat "catalina.sh run" 2 minutes ago Exited (143) About a minute ago friendly_clarke
[root@localhost /]# systemctl restart docker
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5557254c6d9 tomcat "catalina.sh run" 18 seconds ago Up 3 seconds 8080/tcp fervent_goldstine
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5557254c6d9 tomcat "catalina.sh run" 26 seconds ago Up 10 seconds 8080/tcp fervent_goldstine
1a7a3b5112fd tomcat "catalina.sh run" 3 minutes ago Exited (143) About a minute ago friendly_clarke
[root@localhost /]#
二、命令修改
在启动时如果没有添加这个参数怎么办呢,比如1a7a3b5112fd这个容器在启动的时候是没有添加–restart=always参数的,针对这种情况我们可以使用命令进行修改。docker container update --restart=always 容器名字
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5557254c6d9 tomcat "catalina.sh run" 26 seconds ago Up 10 seconds 8080/tcp fervent_goldstine
1a7a3b5112fd tomcat "catalina.sh run" 3 minutes ago Exited (143) About a minute ago friendly_clarke
[root@localhost /]# docker container update --restart=always 1a7a3b5112fd
1a7a3b5112fd
[root@localhost /]# systemctl restart docker
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5557254c6d9 tomcat "catalina.sh run" 7 minutes ago Up 3 seconds 8080/tcp fervent_goldstine
1a7a3b5112fd tomcat "catalina.sh run" 9 minutes ago Up 3 seconds 8080/tcp friendly_clarke
[root@localhost /]#
三、修改配置文件
除了上面两种方式之后还可以通过修改容器的配置文件来修尽管这个参数,首先停止容器,不然无法修改配置文件
配置默认文件路径为:/var/lib/docker/containers/容器ID
在该目录下找到一个文件 hostconfig.json ,找到该文件中关键字 RestartPolicy
修改前配置:“RestartPolicy”:{“Name”:“no”,“MaximumRetryCount”:0}
修改后配置:“RestartPolicy”:{“Name”:“always”,“MaximumRetryCount”:0}
最后启动容器。
[root@localhost /]# docker container update --restart=no 1a7a3b5112fd
1a7a3b5112fd
[root@localhost /]# systemctl restart docker
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5557254c6d9 tomcat "catalina.sh run" 11 minutes ago Up 2 seconds 8080/tcp fervent_goldstine
1a7a3b5112fd tomcat "catalina.sh run" 14 minutes ago Exited (143) 3 seconds ago friendly_clarke
[root@localhost ~]# cd /docker/containers/1a7a3b5112fd310dc70903df479203a81d7d64f7c19ea0aca8fff8ece10e04a4/
[root@localhost 1a7a3b5112fd310dc70903df479203a81d7d64f7c19ea0aca8fff8ece10e04a4]# ls
1a7a3b5112fd310dc70903df479203a81d7d64f7c19ea0aca8fff8ece10e04a4-json.log config.v2.json hostname mounts resolv.conf.hash
checkpoints hostconfig.json hosts resolv.conf
[root@localhost 1a7a3b5112fd310dc70903df479203a81d7d64f7c19ea0aca8fff8ece10e04a4]# more hostconfig.json
{"Binds":null,"ContainerIDFile":"","LogConfig":{"Type":"json-file","Config":{}},"NetworkMode":"default","PortBindings":{},"RestartPolicy":{"Name":"no","MaximumR
etryCount":0},"AutoRemove":false,"VolumeDriver":"","VolumesFrom":null,"CapAdd":null,"CapDrop":null,"Capabilities":null,"Dns":[],"DnsOptions":[],"DnsSearch":[],"Extr
aHosts":null,"GroupAdd":null,"IpcMode":"private","Cgroup":"","Links":null,"OomScoreAdj":0,"PidMode":"","Privileged":false,"PublishAllPorts":false,"ReadonlyRootfs":f
alse,"SecurityOpt":null,"UTSMode":"","UsernsMode":"","ShmSize":67108864,"Runtime":"runc","ConsoleSize":[0,0],"Isolation":"","CpuShares":0,"Memory":0,"NanoCpus":0,"C
groupParent":"","BlkioWeight":0,"BlkioWeightDevice":[],"BlkioDeviceReadBps":null,"BlkioDeviceWriteBps":null,"BlkioDeviceReadIOps":null,"BlkioDeviceWriteIOps":null,"
CpuPeriod":0,"CpuQuota":0,"CpuRealtimePeriod":0,"CpuRealtimeRuntime":0,"CpusetCpus":"","CpusetMems":"","Devices":[],"DeviceCgroupRules":null,"DeviceRequests":null,"
KernelMemory":0,"KernelMemoryTCP":0,"MemoryReservation":0,"MemorySwap":0,"MemorySwappiness":null,"OomKillDisable":false,"PidsLimit":null,"Ulimits":null,"CpuCount":0
,"CpuPercent":0,"IOMaximumIOps":0,"IOMaximumBandwidth":0,"MaskedPaths":["/proc/asound","/proc/acpi","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/timer_li
st","/proc/timer_stats","/proc/sched_debug","/proc/scsi","/sys/firmware"],"ReadonlyPaths":["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]}
[root@localhost 1a7a3b5112fd310dc70903df479203a81d7d64f7c19ea0aca8fff8ece10e04a4]# sed -i 's/"RestartPolicy":{"Name":"no"/"RestartPolicy":{"Name":"always"/g' hostconfig.json
[root@localhost 1a7a3b5112fd310dc70903df479203a81d7d64f7c19ea0aca8fff8ece10e04a4]# more hostconfig.json
{"Binds":null,"ContainerIDFile":"","LogConfig":{"Type":"json-file","Config":{}},"NetworkMode":"default","PortBindings":{},"RestartPolicy":{"Name":"always","MaximumR
etryCount":0},"AutoRemove":false,"VolumeDriver":"","VolumesFrom":null,"CapAdd":null,"CapDrop":null,"Capabilities":null,"Dns":[],"DnsOptions":[],"DnsSearch":[],"Extr
aHosts":null,"GroupAdd":null,"IpcMode":"private","Cgroup":"","Links":null,"OomScoreAdj":0,"PidMode":"","Privileged":false,"PublishAllPorts":false,"ReadonlyRootfs":f
alse,"SecurityOpt":null,"UTSMode":"","UsernsMode":"","ShmSize":67108864,"Runtime":"runc","ConsoleSize":[0,0],"Isolation":"","CpuShares":0,"Memory":0,"NanoCpus":0,"C
groupParent":"","BlkioWeight":0,"BlkioWeightDevice":[],"BlkioDeviceReadBps":null,"BlkioDeviceWriteBps":null,"BlkioDeviceReadIOps":null,"BlkioDeviceWriteIOps":null,"
CpuPeriod":0,"CpuQuota":0,"CpuRealtimePeriod":0,"CpuRealtimeRuntime":0,"CpusetCpus":"","CpusetMems":"","Devices":[],"DeviceCgroupRules":null,"DeviceRequests":null,"
KernelMemory":0,"KernelMemoryTCP":0,"MemoryReservation":0,"MemorySwap":0,"MemorySwappiness":null,"OomKillDisable":false,"PidsLimit":null,"Ulimits":null,"CpuCount":0
,"CpuPercent":0,"IOMaximumIOps":0,"IOMaximumBandwidth":0,"MaskedPaths":["/proc/asound","/proc/acpi","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/timer_li
st","/proc/timer_stats","/proc/sched_debug","/proc/scsi","/sys/firmware"],"ReadonlyPaths":["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]}
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5557254c6d9 tomcat "catalina.sh run" 14 minutes ago Up 2 seconds 8080/tcp fervent_goldstine
1a7a3b5112fd tomcat "catalina.sh run" 17 minutes ago Up 2 seconds 8080/tcp friendly_clarke
更多推荐
所有评论(0)