,自动更新Docker镜像和容器需要以下四步:停止容器:dockerstop删除容器:dockerrm更新镜像:dockerpull启动容器:dockerrun...停止容器这一步可以在删除容器时使用-f参数代替,尽管它仍然需要三个步骤。如果部署了大量的容器,需要更新,这种传统方式的工作量是巨大的。Watchtower是一个实用程序,可以自动更新Docker基础映像和容器。它监视正在运行的容器和相关图像。当检测到registry中的镜像与本地镜像不同时,它会拉取最新的镜像,并以与最初部署时相同的参数重启相应的容器。一切似乎都很好。没有发生,就像更新手机上的应用程序一样。快速入门Watchtower本身被打包为Docker镜像,因此它可以像任何其他容器一样运行:dockerrun-d\--namewatchtower\-v/var/run/docker.sock:/var/run/docker.sock\containrrr/watchtower和所有容器都会自动更新,包括Watchtower本身。选项参数$dockerrun--rmcontainrrr/watchtower-hWatchtowerautomaticallyupdatesrunningDockercontainerswheneveranewimageisreleased.Moreinformationavailableatathttps://github.com/containrrr/watchtower/.Usage:watchtower[flags]Flags:-a,--api-versionstringapiversiontousebydockerclient(default"1.24")-c,--cleanupremovepreviouslyusedimagesafterupdating-d,--debugenableddebugmodewithverboselogging--enable-lifecycle-hooksEnabletheexecutionofcommandstriggeredbypre-andpost-updatelifecyclehooks-h,--helphelpforwatchtower-H,--hoststringdaemonsockettoconnectto(default"unix:///var/run/docker.sock")-S,--include-stoppedWillalsoincludecreatedandexitedcontainers-i,--intervalintpollinterval(inseconds)(default300)-e,--label-enablewatchcontainerswherethecom.centurylinklabs.watchtower.enablelabelistrue-m,--monitor-onlyWillonlymonitorfornewimages,notupdatethecontainers--no-pull不pullanynewimages--no-restartdonotrestartanycontainers--notification-email-delayintD延迟发送通知,以秒为单位--notification-email-fromstringAddresstosendnotificationemailsfrom--notification-email-serverstringSMTPservertosendnotificationemailsthrough--notification-email-server-passwordstringSMTPserverpasswordforsendingnotifications--notification-email-server-portintSMTPserverporttosendnotificationemailsthrough(default25)--notification-email-skip-server-tlsverifyControlswhetherwatchtowerverifiestheSMTPserver'scertificatechainandhostname.Shouldonlybeusedfortesting.--notification-email-server-userstringSMTPserveruserforsendingnotifications--notification-email-subjecttagstringSubjectprefixtagfornotificationsviamail--notification-email-tostringAddresstosendnotificationemailsto--notification-gotify-tokenstringTheGotifyApplicationrequiredtoquerytheGotifyAPI--notification-gotify-urlstringTheGotifyURLtosendnotificationsto--notification-msteams-dataTheMSTeamsnotifierwilltrytoextractlogentryfieldsasMSTeamsmessagefacts--notification-msteams-hookstringTheMSTeamsWebHookURL将通知发送到--notification-slack-channelstringAstring,它覆盖了webhook的默认通道。示例:#my-custom-channel--notification-slack-hook-urlstringTheSlackHookURL将通知发送到--notification-slack-icon-emojistringAnemoji-codecodestringicontouseinicon-urlstringAniconimageURLstringtouseinplaceofthedefaulticon--notification-slack-identifierstringAstringwhichwillbeusedtoidentifythemessagescomingfromthiswatchtowerinstance(default"watchtower")-n,--notificationsstringsnotificationtypestosend(valid:email,slack,msteams,gotify)--notifications-levelstringTheloglevelusedforsendingnotifications,error,dewarbus,falgiblevalues(默认“信息”)--remove-volumesremoveattachedvolumesbeforeupdating--revive-stoppedWillalsostartstoppedcontainersthatwereupdated,ifinclude-stoppedisactive-R,--run-onceRunoncenowandexit-s,--schedulestring定义何时更新的cron表达式-t,--stop-timeoutdurationtimeoutbeforeacontainerisforcefullystopped(default10s)-v,--tlsverifyuseTLSandverifytheremotautomaticallyclearoldimages官方给出的默认启动命令会积累很多标签为none的旧镜像长期使用后,如果无人看管,会占用大量磁盘空间为了避免这种磁盘空间很大的情况,可以添加--cleanup选项,这样每次更新都会清理旧镜像。dockerrun-d\--namewatchtower\--restartunless-stopped\-v/var/run/docker.sock:/var/run/docker.sock\containrrr/watchtower\--cleanup--cleanup选项可以简写为-c:dockerrun-d\--namewatchtower\--restartunless-stopped\-v/var/run/docker.sock:/var/run/docker.sock\containrrr/watchtower-c选择性自动更新一些容器可能需要稳定频繁更新或重启可能会导致一些问题。这时候我们可以通过一些选项参数来选择和控制容器的更新。容器更新列表假设我们只想更新nginx和redis这两个容器,我们可以在启动命令的末尾追加容器名,如下例:dockerrun-d\--namewatchtower\--restartunless-stopped\-v/var/run/docker.sock:/var/run/docker.sock\containrrr/watchtower-c\nginxredis博主觉得把需要更新的容器的名字写在启动命令里不太行有利于管理,于是他想到了一种更好的方法,建立一个更新列表文件。$cat~/.watchtower.listaria2-prounlockmusicmtg...通过变量调用此列表:dockerrun-d\--namewatchtower\--restartunless-stopped\-v/var/run/docker.sock:/var/run/docker.sock\containrrr/watchtower-c\$(cat~/.watchtower.list)这样只需要在调整列表后删除Watchtower容器,重新执行上述命令即可重启Watchtower。2.设置单个容器的自动更新特性为容器添加LABELcom.centurylinklabs.watchtower.enable并设置其值为false,或者在容器中添加--labelcom.centurylinklabs.watchtower.enable=false参数启动命令排除相应的容器。下面的例子是博主的openwrt-mini镜像的容器启动命令,Watchtower会一直忽略它的更新,即使它被列入自动更新列表。dockerrun-d\--nameopenwrt-mini\--restartalways\--networkopenwrt\--privileged\--labelcom.centurylinklabs.watchtower.enable=false\p3terx/openwrt-mini\/sbin/init加入容器启动命令--labelcom.centurylinklabs.watchtower.enable=true参数,并且在Watchtower添加--label-enable选项时,Watchtower只会更新这些包含该参数的容器。dockerrun-d\--namewatchtower\--restartunless-stopped\-v/var/run/docker.sock:/var/run/docker.sock\containrrr/watchtower-c\--label-enable--label-enable可以简写为-e:dockerrun-d\--namewatchtower\--restartunless-stopped\-v/var/run/docker.sock:/var/run/docker.sock\containrrr/watchtower-ce因为需要是在容器启动的时候设置的,设置之后不能直接更改,只能重新构建容器,所以这种方式没有updatelist方式灵活。尤其是设置了com.centurylinklabs.watchtower.enable=false参数后,container会一直被watchtower忽略,包括后面会提到的手动更新方式,所以一般不推荐,除非你愿意手动rebuildnative更新方式。设置自动更新检查的频率。默认情况下,Watchtower将每5分钟轮询一次。如果你觉得这个频率太高,你可以使用下面的选项来控制更新检查的频率,但是你只能二选一。--interval,-i-以秒为单位设置更新检测间隔。例如,每小时检查一次更新:dockerrun-d\--namewatchtower\--restartunless-stopped\-v/var/run/docker.sock:/var/run/docker.sock\containrrr/watchtower-c\--interval3600--schedule,-s-设置检查更新的时间。该格式是一个6字段的cron表达式,而不是传统的5字段,其中第一个数字是第二个数字。例如,每天凌晨2点检查更新:dockerrun-d\--namewatchtower\--restartunless-stopped\-v/var/run/docker.sock:/var/run/docker.sock\containrrr/watchtower-c\---schedule"002***"Manualupdate之前的用法是让Watchtower以detached(后台)模式运行,自动更新容器,而Watchtower也支持在foreground(前台)模式下使用,即运行一次退出并删除删除容器以手动更新容器。这对于偶尔更新不在自动更新列表中的容器很有用。对于前台模式,您需要添加特殊选项--run-once。下面的例子会运行一次Watchtower并检查aria2-pro容器的基础镜像更新,最后删除本次运行创建的Watchtower容器。dockerrun--rm\-v/var/run/docker.sock:/var/run/docker.sock\containrrr/watchtower-c\--run-once\aria2-pro--run-once可以简写为-R:dockerrun--rm\-v/var/run/docker.sock:/var/run/docker.sock\containrrr/watchtower-cR\aria2-pro需要注意的是这个容器设置的时候com.centurylinklabs。岗楼。enable=false参数不会被更新。以上是博主在使用守望台中总结的一些使用方法和方法。当然,它还有一些其他的功能和使用方法,比如邮件通知、监控私有registry镜像、更新远程主机上的容器等,这些对于一般用户来说,可能很少用到,就不赘述了这里。感兴趣的朋友可以研究一下守望台官方文档。