Watchtower自动更新容器镜像

对开发运维人员来说,保持 Docker 容器为最新版本是重要工作之一。手动更新 Docker 容器是一项耗时的工作。这篇文章解释了 Watchtower 是什么,如何安装它,以及在 Linux 中如何 使用 Watchtower 自动更新正在运行的 Docker 容器

Watchtower 是什么?

Watchtower 是一款自由开源的应用,用来监控运行中的 Docker 容器,并且当它发现基础镜像被更改后,可以自动的更新容器。

若 Watchtower 发现一个运行中的容器需要更新,它会以发送 SIGTERM 信号的方式,优雅的结束运行中容器的运行。

它会下载新镜像,然后以最初部署时使用的方式,重启容器。所有文件会在后台自动下载,因此不需要用户的介入。

在这份指南中,我们将会明白如何在类 Unix 系统中使用 Watchtower 自动更新正在运行的 Docker 容器。

官网:https://containrrr.dev/watchtower/

传统方式 Watchtower
耗时费力。

【1】stop,停止容器;rm删除容器(docker compose启容器也只会缩减一步)
【2】rmi删除旧镜像
【3】pull拉取新镜像
【4】重新启动容器 | 能自动拉取最新的docker镜像并将其自动运行,能在很大程度上减少运维的工作量 |

watchtower部署在主服务器,通过次服务器进行新镜像的build以及push到私仓

1、Watchtower部署(本次采用手动更新)

【1】手动方式,次服务器通过ssh远端控制主服务器更新容器镜像,存在不安全隐患
【2】采用设置更新频率的方式,对于特定需求下,存在更新不及时的情况
【3】可采用HTTP API模式,通过HTTP请求及时触发容器镜像的更新

  1. vim compose.yml
  2. version: "3.9"
  3. services:
  4. watchtower:
  5. image: containrrr/watchtower
  6. container_name: watchtower
  7. deploy:
  8. restart_policy:
  9. condition: on-failure
  10. delay: 5s
  11. window: 60s
  12. environment:
  13. - WATCHTOWER_TIMEOUT=300s
  14. #- WATCHTOWER_HTTP_API_TOKEN=mytoken
  15. labels:
  16. - "com.centurylinklabs.watchtower.enable=false"
  17. volumes:
  18. - /root/.docker/config.json:/config.json
  19. - /var/run/docker.sock:/var/run/docker.sock
  20. ports:
  21. - 18080:8080
  22. privileged: true
  23. cap_add:
  24. - ALL
  25. command: --cleanup --label-enable --run-once #--http-api-periodic-polls --interval 60
  26. --run-once #采用手动(容器启动一次,自动退出)
  27. --interval 60 #自动更新检查频率方式,每60秒自动检测一次更新
  28. *非手动模式,容器重启策略建议always
  29. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  30. 若采用HTTP API模式,仅允许由 HTTP 请求触发映像更新 --http-api-update
  31. 参考:https://containrrr.dev/watchtower/http-api-mode/
  32. 若开始上述环境变量- WATCHTOWER_HTTP_API_TOKEN=mytoken
  33. 次服务器(或任意可访问主服务器的服务器)通过
  34. curl -H "Authorization: Bearer mytoken" 主服务器IP:18080/v1/update
  35. 发送HTTP请求,触发主服务器的watchtower进行容器镜像的更新
  36. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2、测试容器准备

  1. vim compose.yml
  2. x-config:
  3. &default-config
  4. deploy:
  5. restart_policy:
  6. condition: always
  7. delay: 5s
  8. window: 60s
  9. labels:
  10. - "com.centurylinklabs.watchtower.enable=true" #true表示更新
  11. environment:
  12. - BIMINI_ENV=PROD
  13. volumes:
  14. - /etc/localtime:/etc/localtime:ro
  15. - /etc/timezone:/etc/timezone:ro
  16. logging:
  17. driver: "json-file"
  18. options:
  19. max-size: 4G
  20. network_mode: host
  21. working_dir: /
  22. services:
  23. test-ubuntu:
  24. << : *default-config
  25. image: 192.168.10.81:8082/test/test-ubuntu:v1
  26. container_name: test-ubuntu
  27. tty: true
  28. 也可
  29. version: "3"
  30. services:
  31. test-ubuntu:
  32. restart: always
  33. image: 192.168.10.81:8082/test/test-ubuntu:v1
  34. container_name: test-ubuntu
  35. tty: true
  36. labels:
  37. - "com.centurylinklabs.watchtower.enable=true" #true表示更新
  38. volumes:
  39. - /etc/localtime:/etc/localtime:ro
  40. - /etc/timezone:/etc/timezone:ro
  41. logging:
  42. driver: "json-file"
  43. options:
  44. max-size: 4G

3、远程工具准备(另一台)

sshpass
安装

  1. $ sudo apt update
  2. $ sudo apt install sshpass

基本使用

  1. $ sshpass -p '123456' ssh cwy@192.168.10.81
  2. ********若没有任何反应,可以通过ssh cwy@192.168.10.81先建立连接

sudo: no tty present and no askpass program specified

  1. visudo
  2. %sudo ALL=(ALL:ALL) ALL
  3. 变为
  4. %sudo ALL=(ALL:ALL) NOPASSWD:ALL

4、编写脚本(另一台)

  1. vim test.sh
  2. #/bin/bash
  3. img=192.168.10.81:8082/test/test-ubuntu
  4. ver=v1
  5. docker push $img:$ver
  6. sleep 2
  7. sshpass -p '123456' ssh cwy@192.168.10.81 'sudo docker compose -f /opt/services/watchtower/compose.yml up -d'
  8. chmod +x test.sh

5、测试(另一台)

  1. #通过以下命令得到镜像
  2. docker build -t 192.168.10.81:8082/test/test-ubuntu:v1 .
  3. #执行脚本
  4. ./test.sh
  5. 观察镜像发生改变

Watchtower自动更新容器镜像 - 图1