推荐一款自动更新 Docker 镜像与容器的神器 Watchtower
公众号关注 「奇妙的 Linux 世界」
设为「星标」,每天带你玩转 Linux !
前言
Docker
容器的部署有一种在手机上装 App
的感觉,但 Docker
容器并不会像手机 App
那样会自动更新,而如果我们需要更新容器一般需要以下四个步骤:
停止容器:
docker stop <CONTAINER>
删除容器:
docker rm <CONTAINER>
更新镜像:
docker pull <IMAGE>
启动容器:
docker run <ARG> ... <IMAGE>
停止容器这个步骤可以在删除容器时使用 -f
参数来代替,即使这样还是需要三个步骤。如果部署了大量的容器需要更新使用这种传统的方式工作量是巨大的。
Watchtower
是一个可以实现自动化更新 Docker
基础镜像与容器的实用工具。它监视正在运行的容器以及相关的镜像,当检测到 registry
中的镜像与本地的镜像有差异时,它会拉取最新镜像并使用最初部署时相同的参数重新启动相应的容器,一切好像什么都没发生过,就像更新手机上的 App
一样。
快速开始
Watchtower
本身被打包为 Docker
镜像,因此可以像运行任何其他容器一样运行它:
docker run -d \--name watchtower \-v /var/run/docker.sock:/var/run/docker.sock \containrrr/watchtower
然后所有容器都会自动更新,也包括 Watchtower 本身。
选项参数
$ docker run --rm containrrr/watchtower -hWatchtower automatically updates running Docker containers whenever a new image is released.
More information available at https://github.com/containrrr/watchtower/.Usage:watchtower [flags]Flags:-a, --api-version string api version to use by docker client (default "1.24")-c, --cleanup remove previously used images after updating-d, --debug enable debug mode with verbose logging--enable-lifecycle-hooks Enable the execution of commands triggered by pre- and post-update lifecycle hooks-h, --help help for watchtower-H, --host string daemon socket to connect to (default "unix:///var/run/docker.sock")-S, --include-stopped Will also include created and exited containers-i, --interval int poll interval (in seconds) (default 300)-e, --label-enable watch containers where the com.centurylinklabs.watchtower.enable label is true-m, --monitor-only Will only monitor for new images, not update the containers--no-pull do not pull any new images--no-restart do not restart any containers--notification-email-delay int Delay before sending notifications, expressed in seconds--notification-email-from string Address to send notification emails from--notification-email-server string SMTP server to send notification emails through--notification-email-server-password string SMTP server password for sending notifications--notification-email-server-port int SMTP server port to send notification emails through (default 25)--notification-email-server-tls-skip-verifyControls whether watchtower verifies the SMTP server's certificate chain and host name.Should only be used for testing.--notification-email-server-user string SMTP server user for sending notifications--notification-email-subjecttag string Subject prefix tag for notifications via mail--notification-email-to string Address to send notification emails to--notification-gotify-token string The Gotify Application required to query the Gotify API--notification-gotify-url string The Gotify URL to send notifications to--notification-msteams-data The MSTeams notifier will try to extract log entry fields as MSTeams message facts--notification-msteams-hook string The MSTeams WebHook URL to send notifications to--notification-slack-channel string A string which overrides the webhook's default channel. Example: #my-custom-channel--notification-slack-hook-url string The Slack Hook URL to send notifications to--notification-slack-icon-emoji string An emoji code string to use in place of the default icon--notification-slack-icon-url string An icon image URL string to use in place of the default icon--notification-slack-identifier string A string which will be used to identify the messages coming from this watchtower instance (default "watchtower")-n, --notifications strings notification types to send (valid: email, slack, msteams, gotify)--notifications-level string The log level used for sending notifications. Possible values: panic, fatal, error, warn, info or debug (default "info")--remove-volumes remove attached volumes before updating--revive-stopped Will also start stopped containers that were updated, if include-stopped is active-R, --run-once Run once now and exit-s, --schedule string the cron expression which defines when to update-t, --stop-timeout duration timeout before a container is forcefully stopped (default 10s)-v, --tlsverify use TLS and verify the remote
自动清除旧镜像
官方给出的默认启动命令在长期使用后会堆积非常多的标签为 none
的旧镜像,如果放任不管会占用大量的磁盘空间。要避免这种情况可以加入 --cleanup
选项,这样每次更新都会把旧的镜像清理掉。
docker run -d \--name watchtower \--restart unless-stopped \-v /var/run/docker.sock:/var/run/docker.sock \containrrr/watchtower \--cleanup
--cleanup
选项可以简写为 -c
:
docker run -d \--name watchtower \--restart unless-stopped \-v /var/run/docker.sock:/var/run/docker.sock \containrrr/watchtower -c
选择性自动更新
某些容器可能需要稳定的运行,经常更新或重启可能会造成一些问题,这时我们可以使用一些选项参数来选择与控制容器的更新。
容器更新列表
假设我们只想更新 nginx
、redis
这两个容器,我们可以把容器名称追加到启动命令的最后面,就像下面这个例子:
docker run -d \--name watchtower \--restart unless-stopped \-v /var/run/docker.sock:/var/run/docker.sock \containrrr/watchtower -c \nginx redis
博主觉得把需要更新的容器名称写在启动命令中不利于管理,于是想了个更好的方法,建立一个更新列表文件。
$ cat ~/.watchtower.list
aria2-pro
unlockmusic
mtg
...
通过变量的方式去调用这个列表:
docker run -d \--name watchtower \--restart unless-stopped \-v /var/run/docker.sock:/var/run/docker.sock \containrrr/watchtower -c \$(cat ~/.watchtower.list)
这样只需要调整列表后删除 Watchtower
容器并重新执行上面的命令重新启动 Watchtower
即可。
设置单个容器自动更新特征
给容器添加 com.centurylinklabs.watchtower.enable
这个 LABEL
并设置它的值为 false
,或者在启动命令中加入 --label com.centurylinklabs.watchtower.enable=false
参数可以排除相应的容器。下面这个例子是博主的 openwrt-mini
镜像的容器启动命令,Watchtower
将永远忽略它的更新,即使它包含在自动更新列表中。
docker run -d \--name openwrt-mini \--restart always \--network openwrt \--privileged \--label com.centurylinklabs.watchtower.enable=false \p3terx/openwrt-mini \/sbin/init
当容器启动命令中加入 --label com.centurylinklabs.watchtower.enable=true
参数,并且给 Watchtower
加上 --label-enable
选项时,Watchtower
将只更新这些包含此参数的容器。
docker run -d \--name watchtower \--restart unless-stopped \-v /var/run/docker.sock:/var/run/docker.sock \containrrr/watchtower -c \--label-enable
--label-enable
可以简写为 -e
:
docker run -d \--name watchtower \--restart unless-stopped \-v /var/run/docker.sock:/var/run/docker.sock \containrrr/watchtower -ce
因为需要在容器启动时进行设置,且设置后就无法直接更改,只能重建容器,所以这种方式的灵活性不如更新列表法。尤其是在设置 com.centurylinklabs.watchtower.enable=false
参数后容器将永远被 Watchtower 忽略,也包括后面将要提到的手动更新方式,所以一般不推荐这样做,除非你愿意手动重建的原生方式更新。
设置自动更新检查频率
默认情况下 Watchtower
每 5
分钟会轮询一次,如果你觉得这个频率太高了可以使用如下选项来控制更新检查的频率,但二者只能选择其一。
--interval
, -i
- 设置更新检测时间间隔,单位为秒。比如每隔 1
个小时检查一次更新:
docker run -d \--name watchtower \--restart unless-stopped \-v /var/run/docker.sock:/var/run/docker.sock \containrrr/watchtower -c \--interval 3600
--schedule
, -s
- 设置定时检测更新时间。格式为 6
字段 Cron
表达式,而非传统的 5
字段,即第一位是秒。比如每天凌晨 2
点检查一次更新:
docker run -d \--name watchtower \--restart unless-stopped \-v /var/run/docker.sock:/var/run/docker.sock \containrrr/watchtower -c \--schedule "0 0 2 * * *"
手动更新
前面的使用方式都是让 Watchtower
以 detached
(后台)模式在运行并自动更新容器,而 Watchtower
也支持以 foreground
(前台)模式来使用,即运行一次退出并删掉容器,来实现手动更新容器。这对于偶尔更新一次那些不在自动更新列表中的容器非常有用。
对于 foreground
模式,需要加上 --run-once
这个专用的选项。下面的例子 Docker
会运行一次 Watchtower
并检查 aria2-pro
容器的基础镜像更新,最后删掉本次运行创建的 Watchtower
容器。
docker run --rm \-v /var/run/docker.sock:/var/run/docker.sock \containrrr/watchtower -c \--run-once \aria2-pro
--run-once
可以简写为 -R
:
docker run --rm \-v /var/run/docker.sock:/var/run/docker.sock \containrrr/watchtower -cR \aria2-pro
需要注意的是当这个容器设置过 com.centurylinklabs.watchtower.enable=false
参数时不会更新。
尾巴
以上是博主在使用 Watchtower
中总结的一些使用方式和方法,当然它还有一些其它的功能与使用方式,比如电子邮件通知、监视私人注册表的镜像、更新远程主机上的容器等,这些对于一般用户来说可能很少会用到,所以这里就不赘述了,感兴趣的小伙伴可以去研究 Watchtower
官方文档。
本文转载自:「P3TERX ZONE」,原文:https://tinyurl.com/y9e4pslr,版权归原作者所有。欢迎投稿,投稿邮箱: editor@hi-linux.com。
你可能还喜欢
点击下方图片即可阅读
分享一份超详细的阿里云内部 Kubernetes 项目实战手册
点击上方图片,打开小程序,加入「玩转 Linux」圈子
更多有趣的互联网新鲜事,关注「奇妙的互联网」视频号全了解!
推荐一款自动更新 Docker 镜像与容器的神器 Watchtower相关推荐
- [watchtower] 自动更新 Docker 镜像与容器
自动更新 Docker 镜像与容器 pull镜像 运行 更新docker镜像 参数解释 更新镜像列表文件 pull镜像 根据自己的系统架构选择,都是为最新版镜像 docker pull contain ...
- Docker、nvidia-container-toolkit安装与常用docker命令及docker镜像和容器的更新维护
Python的virtual env和Anaconda的env可以用来做环境隔离防止不同的模型环境之间在安装时相互影响甚至因为支持包的版本冲突导致不能同时使用,但是不具备打包发布部署的功能,想要调试好 ...
- Docker学习二:Docker镜像与容器
前言 本次学习来自于datawhale组队学习: 教程地址为: https://github.com/datawhalechina/team-learning-program/tree/master/ ...
- docker镜像及容器的使用
docker镜像及容器的使用 一.Docker三大概念 docker 中有三个核心概念:镜像,容器,仓库. 因此准确把握这三大概念对于掌握docker技术尤为重要. 1.镜像 Docker镜像就相当于 ...
- 安装docker与docker镜像和容器基本的基本操作
安装docker与docker镜像和容器基本的基本操作 前言 一.docker安装步骤详解 二.网络优化 三.Docker-server端配置文件建议配置 四.Doceker基本命令(运维) (1)查 ...
- docker 一起性重启所有镜像_docker系列四之docker镜像与容器的常用命令
docker镜像与容器的常用命令 一.概述 docker的镜像于容器是docker中两个至关重要的概念,首先给各位读者解释一下笔者对于这两个概念的理解.镜像,我们从字面意思上看,镜子里成像,我们人站在 ...
- docker镜像和容器的导出导入
docker镜像的导出和导入 docker镜像和容器的导入导出,用于迁移.备份.升级等场景.主要用到export.import.save.load四个方法. 显示当前docker中的镜像: docke ...
- Docker镜像与容器基本操作
Docker镜像与容器基本操作 一.Docker基本命令 1.查看docker版本 2.搜索镜像和dockerhub 3.下载镜像 4.docker run 5.查看所有状态容器 二.docker镜像 ...
- Docker镜像和容器
docker镜像与容器概念 本文转载自:https://www.cnblogs.com/bethal/p/5945038.html 本文用图文并茂的方式介绍了容器.镜像的区别和Docker每个命令后面 ...
最新文章
- 使用angular2 自带的指令,快捷创建服务,组件等!
- linux修改目录block信息,linux下文件操作inode,block的变化
- python显示图像某列的颜色值_Python Pandas Matplotlib图由单列中定义的类型值着色
- imagecomposition工程分析
- mysql中约束_【MySQL】:MySQL中四大约束
- Linux社区关于链表的bug讨论我们要看一下
- l4 l7 代理_什么是四层(L4 proxy)和七层负载均衡(L7 proxy)?区别是什么? 翻译自Nginx官网...
- java模拟器百度_Java模拟实现百度文档在线浏览
- 做了三年Java,java参考文献近五年图书
- 接口自动化第四篇----应用工厂模式下的HttpClient请求
- 浪潮服务器【磁盘阵列】故障修复
- 一家化工厂的数字化三级跳 | 产研案例
- LZY的计算器(暴力)
- 浙江大学计算机学院 陈曦,陈曦-计算机与通信工程学院
- SQL Server 数据库(高级)完结篇
- 阿里云短信验证服务详细基础教程
- 《深度强化学习实践》学习内容整理
- 写给仿真软件研发的“一篇文章入门”系列(终)
- SqlSugar操作MySQL数据库
- msvcp120.dll丢失的解决方法(仅一步解决)
热门文章
- 一对同居男女同一天的日记
- 超酷的HTML5 Canvas网络画板教程
- 项目经理之项目经理应该做什么
- ubuntu平台下编译vlc-android视频播放器实践
- UIImageView绘制圆形图片
- ubuntu登录桌面后只显示壁纸,排查方案
- Unity3D 光照系统(一)
- Higgs全球区块链投融资交流会(香港站)成功举办,路演项目备受瞩目
- pycharm提示无法加载文件 C:\Users\admin\Desktop\pythonLX\venv\Scripts\activate.ps1,因为在此系统上禁止运行脚本
- VR全景图片,助力VR全景制作,720全景效果图