注册

使用watchtower更新docker容器

更新方式


定时更新(默认)


执行以下命令后,Watchtower 会在后台每 24 小时自动检查并更新所有运行中的容器:


docker run -d \
--name watchtower \
-v /var/run/docker.sock:/var/run/docker.sock \
containrrr/watchtower

手动立即更新


添加 --run-once 参数启动临时容器,检查更新后自动退出,适合按需触发:


docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock \
containrrr/watchtower --run-once



更新指定容器


在命令末尾添加需要监控的容器名称,多个容器用空格分隔。例如仅监控 nginxredis 容器:


docker run -d \
--name watchtower \
-v /var/run/docker.sock:/var/run/docker.sock \
containrrr/watchtower nginx redis



简化命令


手动更新时,如果使用上面的命令未免太麻烦了,所以我们可以将更新命令设置为别名:


将下面的命令放到对应shell的环境文件中(比如bash对应~/.bashrc,zsh对应~/.zshrc


alias update-container="docker run --rm -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower --run-once"

编辑完环境文件后,重新打开命令窗口,或使用source ~/.bashrcsource ~/.zshrc加载命令。
然后就可以通过下面的方式更新容器了:


update-container 容器标识

比如:


update-container nginx-ui-latest

image.png


作者:OrionZephyr
来源:juejin.cn/post/7541682368329170954

0 个评论

要回复文章请先登录注册