Watchtower—自动更新docker镜像与容器
创始人
2025-05-28 21:13:58

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 -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

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登录阿里云的私有镜像服务后会自动生成,生成的位置应该是在登录后的根目录

相关内容

热门资讯

关于抚顺市房屋征收与补偿条例,... 第一条 为规范我市国有土地上房屋征收与补偿活动,维护公共利益,保障被征收房屋所权人的合法权益,根据...
关于鞍山市房屋征收与补偿条例,... 一、拆迁住房以拆迁当时的合法住房为安置依据,凡在动迁以前,取得合法居住房屋的住户,均按原居住合法居室...
关于大连市房屋征收与补偿条例,... 最新或2023(历届)大连房屋征收补偿  日前,市政府网站发布《大连市市区国有土地上房屋征收补偿安置...
关于沈阳市房屋征收与补偿条例,... 最新或2023(历届)沈阳房屋征收补偿  沈阳市出台《国有土地上房屋征收与补偿办法》  被征收人可自...
最新或2023(历届)国考职位...   最新或2023(历届)国考职位表公布 国考公告三大亮点解读   国家公务员局网站今日发布中央机关...