×

docker重启参数--restart=always的作用

hqy hqy 发表于2025-01-14 21:36:48 浏览4 评论0

抢沙发发表评论

mportant;"> --restart=always参数能够使我们在重启docker时,自动启动相关容器。
Docker容器的重启策略如下:
no,默认策略,在容器退出时不重启容器
on-failure,在容器非正常退出时(退出状态非0),才会重启容器
on-failure:3,在容器非正常退出时重启容器,最多重启3次
always,在容器退出时总是重启容器
unless-stopped,在容器退出时总是重启容器,但是不考虑在Docker守护进程启动时就已经停止了的容器
参考:Docker容器的重启策略及docker run的--restart选项详解_易生一世的博客-CSDN博客_restart: unless-stopped

    首先我们先看下正常启动容器不加参数的情况重启docker会出现什么情况

一、启动时候添加参数

1、不加–restart=always参数启动容器

[root@localhost /]# docker run -d tomcat1a7a3b5112fd310dc70903df479203a81d7d64f7c19ea0aca8fff8ece10e04a4[root@localhost /]# docker psCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES1a7a3b5112fd        tomcat              "catalina.sh run"   29 seconds ago      Up 28 seconds       8080/tcp            friendly_clarke[root@localhost /]# docker ps -aCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES1a7a3b5112fd        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 psCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES[root@localhost /]# docker ps -aCONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                       PORTS               NAMES1a7a3b5112fd        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 tomcatc5557254c6d90b1ba8f9a93ff66e6a28f172e92a56cf945af533327f38152fbc[root@localhost /]# docker psCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMESc5557254c6d9        tomcat              "catalina.sh run"   4 seconds ago       Up 3 seconds        8080/tcp            fervent_goldstine[root@localhost /]# docker ps -aCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                            PORTS               NAMESc5557254c6d9        tomcat              "catalina.sh run"   8 seconds ago       Up 7 seconds                      8080/tcp            fervent_goldstine1a7a3b5112fd        tomcat              "catalina.sh run"   2 minutes ago       Exited (143) About a minute ago                       friendly_clarke[root@localhost /]# systemctl restart docker[root@localhost /]# docker psCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMESc5557254c6d9        tomcat              "catalina.sh run"   18 seconds ago      Up 3 seconds        8080/tcp            fervent_goldstine[root@localhost /]# docker ps -aCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                            PORTS               NAMESc5557254c6d9        tomcat              "catalina.sh run"   26 seconds ago      Up 10 seconds                     8080/tcp            fervent_goldstine1a7a3b5112fd        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 -aCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                            PORTS               NAMESc5557254c6d9        tomcat              "catalina.sh run"   26 seconds ago      Up 10 seconds                     8080/tcp            fervent_goldstine1a7a3b5112fd        tomcat              "catalina.sh run"   3 minutes ago       Exited (143) About a minute ago                       friendly_clarke[root@localhost /]# docker container update --restart=always 1a7a3b5112fd1a7a3b5112fd[root@localhost /]# systemctl restart docker[root@localhost /]# docker ps -aCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMESc5557254c6d9        tomcat              "catalina.sh run"   7 minutes ago       Up 3 seconds        8080/tcp            fervent_goldstine1a7a3b5112fd        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 1a7a3b5112fd1a7a3b5112fd[root@localhost /]# systemctl restart docker[root@localhost /]# docker ps -aCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMESc5557254c6d9        tomcat              "catalina.sh run"   11 minutes ago      Up 2 seconds                 8080/tcp            fervent_goldstine1a7a3b5112fd        tomcat              "catalina.sh run"   14 minutes ago      Exited (143) 3 seconds ago                       friendly_clarke[root@localhost ~]# cd /docker/containers/1a7a3b5112fd310dc70903df479203a81d7d64f7c19ea0aca8fff8ece10e04a4/[root@localhost 1a7a3b5112fd310dc70903df479203a81d7d64f7c19ea0aca8fff8ece10e04a4]# ls1a7a3b5112fd310dc70903df479203a81d7d64f7c19ea0aca8fff8ece10e04a4-json.log  config.v2.json   hostname  mounts       resolv.conf.hashcheckpoints                                                                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","MaximumRetryCount":0},"AutoRemove":false,"VolumeDriver":"","VolumesFrom":null,"CapAdd":null,"CapDrop":null,"Capabilities":null,"Dns":[],"DnsOptions":[],"DnsSearch":[],"ExtraHosts":null,"GroupAdd":null,"IpcMode":"private","Cgroup":"","Links":null,"OomScoreAdj":0,"PidMode":"","Privileged":false,"PublishAllPorts":false,"ReadonlyRootfs":false,"SecurityOpt":null,"UTSMode":"","UsernsMode":"","ShmSize":67108864,"Runtime":"runc","ConsoleSize":[0,0],"Isolation":"","CpuShares":0,"Memory":0,"NanoCpus":0,"CgroupParent":"","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_list","/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","MaximumRetryCount":0},"AutoRemove":false,"VolumeDriver":"","VolumesFrom":null,"CapAdd":null,"CapDrop":null,"Capabilities":null,"Dns":[],"DnsOptions":[],"DnsSearch":[],"ExtraHosts":null,"GroupAdd":null,"IpcMode":"private","Cgroup":"","Links":null,"OomScoreAdj":0,"PidMode":"","Privileged":false,"PublishAllPorts":false,"ReadonlyRootfs":false,"SecurityOpt":null,"UTSMode":"","UsernsMode":"","ShmSize":67108864,"Runtime":"runc","ConsoleSize":[0,0],"Isolation":"","CpuShares":0,"Memory":0,"NanoCpus":0,"CgroupParent":"","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_list","/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 -aCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMESc5557254c6d9        tomcat              "catalina.sh run"   14 minutes ago      Up 2 seconds        8080/tcp            fervent_goldstine1a7a3b5112fd        tomcat              "catalina.sh run"   17 minutes ago      Up 2 seconds        8080/tcp            friendly_clarke

参考自:docker重启参数--restart=always的作用_bjywxc的博客-CSDN博客_docker restart=always


打赏

本文链接:https://www.kinber.cn/post/4634.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

 您阅读本篇文章共花了: 

群贤毕至

访客