Watchtower—自动更新docker镜像与容器

这篇具有很好参考价值的文章主要介绍了Watchtower—自动更新docker镜像与容器。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1. 简述

Watchtower 是一个可以实现自动化更新 Docker 基础镜像与容器的实用工具。它监视正在运行的容器以及相关的镜像,当检测到 reg­istry 中的镜像与本地的镜像有差异时,它会拉取最新镜像并使用最初部署时相同的参数重新启动相应的容器,

2. 启动

Watch­tower 本身被打包为 Docker 镜像,因此可以像运行任何其他容器一样运行它:

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

然后所有容器都会自动更新,也包括 Watch­tower 本身。

选项参数:

$ docker run --rm containrrr/watchtower -h

Watchtower 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-verify
                                                    Controls 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

3. 使用示例

3.1 自动清除旧镜像

官方给出的默认启动命令在长期使用后会堆积非常多的标签为 none 的旧镜像,如果放任不管会占用大量的磁盘空间。要避免这种情况可以加入 --cleanup 选项,这样每次更新都会把旧的镜像清理掉。

docker run -d \
    --name watchtower \
    --restart unless-stopped \
    -v /var/run/docker.sock:/var/run/docker.sock \
    containrrr/watchtower -c

3.2 选择性自动更新

某些容器可能需要稳定的运行,经常更新或重启可能会造成一些问题,可以使用一些选项参数来选择与控制容器的更新。

  1. 容器更新列表

    # 只更新 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)
    
    
  2. 设置单个容器自动更新特征

给容器添加 com.centurylinklabs.watchtower.enable 这个 LA­BEL 并设置它的值为 false,或者在启动命令中加入 --label com.centurylinklabs.watchtower.enable=false 参数可以排除相应的容器。

# openwrt-mini 镜像的容器启动命令,Watch­tower 将永远忽略它的更新,即使它包含在自动更新列表中
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 参数,
#并且给 Watch­tower 加上 --label-enable 选项时,Watch­tower 将只更新这些包含此参数的容器
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 参数后容器将永远被 Watch­tower 忽略,包括后面将要提到的手动更新方式,所以一般不推荐这样做,除非你愿意手动重建的原生方式更新

3.3 仅监控更新情况,不更新

使用 --monitor-only 将仅监控新镜像并发送通知,不会更新容器。

4. 设置自动更新检查频率

默认情况下 Watch­tower5 分钟会轮询一次,可以使用:--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 * * *"

5. 手动更新

加上 --run-once 这个专用的选项,运行一次退出并删掉容器,来实现手动更新容器。这对于偶尔更新一次那些不在自动更新列表中的容器非常有用。

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 参数时不会更新。

6. 发送提示

可以通过电子邮件、Slack 、MSTeams 以及 Gotify 发送通知

docker run -d \
  --name watchtower \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -e WATCHTOWER_NOTIFICATIONS=email \
  -e WATCHTOWER_NOTIFICATION_EMAIL_FROM=fromaddress@gmail.com \
  -e WATCHTOWER_NOTIFICATION_EMAIL_TO=toaddress@gmail.com \
  -e WATCHTOWER_NOTIFICATION_EMAIL_SERVER=smtp.gmail.com \
  -e WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PORT=587 \
  -e WATCHTOWER_NOTIFICATION_EMAIL_SERVER_USER=fromaddress@gmail.com \
  -e WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD=app_password \
  -e WATCHTOWER_NOTIFICATION_EMAIL_DELAY=2 \
  containrrr/watchtower

7. watchtower获取私有镜像服务的认证方式

docker run -d --name watchtower --restart always -v /root/.docker/config.json:/config.json -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower -c

/root/.docker/config.json文件,在docker登录阿里云的私有镜像服务后会自动生成,生成的位置应该是在登录后的根目录文章来源地址https://www.toymoban.com/news/detail-469157.html

到了这里,关于Watchtower—自动更新docker镜像与容器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Jenkins+GitLab+Docker搭建前端自动化构建镜像容器部署(无本地证书,映射证书)

    前言 🚀 需提前安装环境及知识点: 1、Docker搭建及基础操作 2、DockerFile文件描述 3、Jenkins搭建及基础点 🚀 目的: 将我们的前端项目打包成一个镜像容器并自动发布部署,可供随时pull访问 1、在当前项目的根目录创建Dockerfile文件并写入如下代码: 代码片段详细描述: 注意

    2024年02月15日
    浏览(40)
  • 记一次线上mysql出错:由于docker自动拉取最新mysql镜像导致mysql容器无法启动

    我随便写写,你们随便看看 环境背景:在docker中部署mysql镜像,通过portainer管理docker容器 简单说下过程:docker里mysql的时区没有设置,导致相差8小时,通过增加TZ=Asiz/Shanghai环境变量,然后重启容器来生效。结果重启的时候始终无法启动起来,后来发现是自动升级了mysql镜像版

    2024年02月07日
    浏览(39)
  • 这里做一篇关于wsl2更新到最新版本使用镜像网络,但是docker创建的容器不能被访问的问题(困扰了我一整天)

    在windows2023年9月的一次更新中,wsl2支持了新的网络模式镜像模式,他能够通过localhost地址从wsl2内部连接到windows的127.0.0.1地址 ##相当于是和本地主机拥有同一个ip 原wsl2是不支持的,虽说是net网络模式,可访问主机,主机也可访问wsl2,同时也可上网,但是不能被网络访问,如

    2024年04月16日
    浏览(40)
  • docker 保存镜像、容器与导入镜像容器

    可以通过以下步骤将Docker镜像导出到另一台计算机上: 在本地计算机上使用以下命令将Docker镜像保存为tar文件: 其中,image-name是要导出的Docker镜像的名称。 将保存的tar文件传输到另一台计算机上,可以使用scp命令或其他文件传输工具。 在目标计算机上使用以下命令将tar文

    2024年02月13日
    浏览(32)
  • docker导出镜像、容器打镜像

    有两种方法,一种是通过容器,一种是通过镜像 1 首先使用 docker ps -a 查看本机上的所有容器 2 导出镜像 使用 docker export 命令根据容器 id 将镜像导成一个文件 上面命令执行之后,我们便可以通过 ls 命令在当前目录下发现 image.tar 3 导入镜像 使用 docker import 命令将这个镜像导

    2024年02月03日
    浏览(39)
  • Docker容器------镜像,容器基础命令

    目录  一,docker镜像  1,查看docker版本  2,获取镜像 2.1查看下载到本地的所有镜像   2.2 获取镜像信息 2.3 添加镜像标签(上传镜像到仓库)   2.4 镜像导出  2.5 镜像导入  2.6 ,删除镜像  2.7 上传镜像 ​编辑 二,docker 容器操作 1,查看容器 2,容器创建----docker create  

    2024年02月05日
    浏览(39)
  • 【Docker系列】容器基础、Docker镜像管理

    个人名片: 对人间的热爱与歌颂,可抵岁月冗长🌞 Github👨🏻‍💻:念舒_C.ying CSDN主页✏️:念舒_C.ying 个人博客🌏 :念舒_C.ying 2.1 Docker镜像查找 命令查找 docker官网查找 2.2 Docker镜像管理 拉取MySQL镜像 查看本地镜像 镜像加速(阿里云) Daocloud镜像站加速 镜像删除 2.3 容器

    2024年01月25日
    浏览(32)
  • docker:容器打包成镜像

    简介 :docker的优势在于,安装部署完成后,将已经运行成功的容器重新打包出新的镜像,即可实现一次构建,处处使用。这需要依赖于docker commit命令 和 docker load来配合完成。 历史攻略: Docker:简介和常用命令分类 sanic:通过dockerfile部署 操作步骤 :以sanic:通过dockerfile部

    2024年02月11日
    浏览(32)
  • docker 镜像/容器常用操作

    目录 1、docker 下载镜像 2、删除镜像 3、新建容器并启动(测试) 4、列出所有正在运行的容器 5、删除容器 6、启动和停止容器 7、查看日志 8、查看元数据(详细数据) 9、将容器中的文件拷贝到主机中 10、进入容器 11、容器之间数据同步 官方文档:docker指令 1、docker 下载镜像 2、

    2024年02月05日
    浏览(30)
  • docker根据镜像生成容器

    要根据 Docker 镜像生成容器,可以使用 docker run 命令。 在上述命令中,你需要将 镜像名称 替换为你想要生成容器的 Docker 镜像的名称。这将使用指定的镜像创建一个新的容器,并启动该容器。 然而,通常情况下,你可能还需要为容器指定其他选项,如端口映射、环境变量、

    2024年02月07日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包