成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

推薦一款自動更新Docker鏡像與容器的神器Watchtower

系統 Linux
Watchtower 是一個可以實現自動化更新 Docker 基礎鏡像與容器的實用工具。

前言

Docker 容器的部署有一種在手機上裝 App 的感覺,但 Docker 容器并不會像手機 App 那樣會自動更新,而如果我們需要更新容器一般需要以下四個步驟:

  •  停止容器:docker stop <CONTAINER>
  •  刪除容器:docker rm <CONTAINER>
  •  更新鏡像:docker pull <IMAGE>
  •  啟動容器:docker run <ARG> ... <IMAGE>

停止容器這個步驟可以在刪除容器時使用 -f 參數來代替,即使這樣還是需要三個步驟。如果部署了大量的容器需要更新使用這種傳統的方式工作量是巨大的。

Watchtower 是一個可以實現自動化更新 Docker 基礎鏡像與容器的實用工具。它監視正在運行的容器以及相關的鏡像,當檢測到 reg­istry 中的鏡像與本地的鏡像有差異時,它會拉取最新鏡像并使用最初部署時相同的參數重新啟動相應的容器,一切好像什么都沒發生過,就像更新手機上的 App 一樣。

快速開始

Watch­tower 本身被打包為 Docker 鏡像,因此可以像運行任何其他容器一樣運行它: 

  1. docker run -d \  
  2.     --name watchtower \  
  3.     -v /var/run/docker.sock:/var/run/docker.sock \  
  4.     containrrr/watchtower 

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

選項參數 

  1. $ docker run --rm containrrr/watchtower -h  
  2. Watchtower automatically updates running Docker containers whenever a new image is released.  
  3. More information available at https://github.com/containrrr/watchtower/.  
  4. Usage:  
  5.   watchtower [flags]  
  6. Flags:  
  7.   -a, --api-version string                          api version to use by docker client (default "1.24")  
  8.   -c, --cleanup                                     remove previously used images after updating  
  9.   -d, --debug                                       enable debug mode with verbose logging  
  10.       --enable-lifecycle-hooks                      Enable the execution of commands triggered by pre- and post-update lifecycle hooks  
  11.   -h, --help                                        help for watchtower  
  12.   -H, --host string                                 daemon socket to connect to (default "unix:///var/run/docker.sock")  
  13.   -S, --include-stopped                             Will also include created and exited containers  
  14.   -i, --interval int                                poll interval (in seconds) (default 300) 
  15.   -e, --label-enable                                watch containers where the com.centurylinklabs.watchtower.enable label is true  
  16.   -m, --monitor-only                                Will only monitor for new images, not update the containers  
  17.       --no-pull                                     do not pull any new images  
  18.       --no-restart                                  do not restart any containers  
  19.       --notification-email-delay int                Delay before sending notifications, expressed in seconds  
  20.       --notification-email-from string              Address to send notification emails from  
  21.       --notification-email-server string            SMTP server to send notification emails through  
  22.       --notification-email-server-password string   SMTP server password for sending notifications  
  23.       --notification-email-server-port int          SMTP server port to send notification emails through (default 25)  
  24.       --notification-email-server-tls-skip-verify  
  25.                                                     Controls whether watchtower verifies the SMTP server's certificate chain and host name.  
  26.                                                     Should only be used for testing.  
  27.       --notification-email-server-user string       SMTP server user for sending notifications  
  28.       --notification-email-subjecttag string        Subject prefix tag for notifications via mail  
  29.       --notification-email-to string                Address to send notification emails to  
  30.       --notification-gotify-token string            The Gotify Application required to query the Gotify API  
  31.       --notification-gotify-url string              The Gotify URL to send notifications to 
  32.       --notification-msteams-data                   The MSTeams notifier will try to extract log entry fields as MSTeams message facts  
  33.       --notification-msteams-hook string            The MSTeams WebHook URL to send notifications to  
  34.       --notification-slack-channel string           A string which overrides the webhook's default channel. Example: #my-custom-channel  
  35.       --notification-slack-hook-url string          The Slack Hook URL to send notifications to  
  36.       --notification-slack-icon-emoji string        An emoji code string to use in place of the default icon  
  37.       --notification-slack-icon-url string          An icon image URL string to use in place of the default icon  
  38.       --notification-slack-identifier string        A string which will be used to identify the messages coming from this watchtower instance (default "watchtower") 
  39.    -n, --notifications strings                        notification types to send (valid: email, slack, msteams, gotify)  
  40.       --notifications-level string                  The log level used for sending notifications. Possible values: panic, fatal, error, warn, info or debug (default "info") 
  41.       --remove-volumes                              remove attached volumes before updating  
  42.       --revive-stopped                              Will also start stopped containers that were updated, if include-stopped is active  
  43.   -R, --run-once                                    Run once now and exit  
  44.   -s, --schedule string                             the cron expression which defines when to update  
  45.   -t, --stop-timeout duration                       timeout before a container is forcefully stopped (default 10s)  
  46.   -v, --tlsverify                                   use TLS and verify the remote 

自動清除舊鏡像

官方給出的默認啟動命令在長期使用后會堆積非常多的標簽為 none 的舊鏡像,如果放任不管會占用大量的磁盤空間。要避免這種情況可以加入 --cleanup 選項,這樣每次更新都會把舊的鏡像清理掉。 

  1. docker run -d \  
  2.     --name watchtower \  
  3.     --restart unless-stopped \  
  4.     -v /var/run/docker.sock:/var/run/docker.sock \  
  5.     containrrr/watchtower \  
  6.     --cleanup 

--cleanup 選項可以簡寫為 -c: 

  1. docker run -d \  
  2.     --name watchtower \  
  3.     --restart unless-stopped \  
  4.     -v /var/run/docker.sock:/var/run/docker.sock \  
  5.     containrrr/watchtower -c 

選擇性自動更新

某些容器可能需要穩定的運行,經常更新或重啟可能會造成一些問題,這時我們可以使用一些選項參數來選擇與控制容器的更新。

  1.  容器更新列表

假設我們只想更新 nginx、redis 這兩個容器,我們可以把容器名稱追加到啟動命令的最后面,就像下面這個例子: 

  1. docker run -d \  
  2.     --name watchtower \  
  3.     --restart unless-stopped \  
  4.     -v /var/run/docker.sock:/var/run/docker.sock \  
  5.     containrrr/watchtower -c \  
  6.     nginx redis 

博主覺得把需要更新的容器名稱寫在啟動命令中不利于管理,于是想了個更好的方法,建立一個更新列表文件。 

  1. $ cat ~/.watchtower.list  
  2. aria2-pro 
  3. unlockmusic  
  4. mtg  
  5. ... 

通過變量的方式去調用這個列表: 

  1. docker run -d \  
  2.     --name watchtower \  
  3.     --restart unless-stopped \  
  4.     -v /var/run/docker.sock:/var/run/docker.sock \  
  5.     containrrr/watchtower -c \  
  6.     $(cat ~/.watchtower.list) 

這樣只需要調整列表后刪除 Watch­tower 容器并重新執行上面的命令重新啟動 Watch­tower 即可。

      2.  設置單個容器自動更新特征

給容器添加 com.centurylinklabs.watchtower.enable 這個 LA­BEL 并設置它的值為 false,或者在啟動命令中加入 --label com.centurylinklabs.watchtower.enable=false 參數可以排除相應的容器。下面這個例子是博主的 openwrt-mini 鏡像的容器啟動命令,Watch­tower 將永遠忽略它的更新,即使它包含在自動更新列表中。 

  1. docker run -d \  
  2.     --name openwrt-mini \  
  3.     --restart always \  
  4.     --network openwrt \  
  5.     --privileged \  
  6.     --label com.centurylinklabs.watchtower.enable=false \  
  7.     p3terx/openwrt-mini \  
  8.     /sbin/init 

當容器啟動命令中加入 --label com.centurylinklabs.watchtower.enable=true 參數,并且給 Watch­tower 加上 --label-enable 選項時,Watch­tower 將只更新這些包含此參數的容器。 

  1. docker run -d \  
  2.     --name watchtower \  
  3.     --restart unless-stopped \  
  4.     -v /var/run/docker.sock:/var/run/docker.sock \  
  5.     containrrr/watchtower -c \  
  6.     --label-enable 

--label-enable 可以簡寫為 -e: 

  1. docker run -d \  
  2.     --name watchtower \  
  3.     --restart unless-stopped \  
  4.     -v /var/run/docker.sock:/var/run/docker.sock \  
  5.     containrrr/watchtower -ce 

因為需要在容器啟動時進行設置,且設置后就無法直接更改,只能重建容器,所以這種方式的靈活性不如更新列表法。尤其是在設置 com.centurylinklabs.watchtower.enable=false 參數后容器將永遠被 Watch­tower 忽略,也包括后面將要提到的手動更新方式,所以一般不推薦這樣做,除非你愿意手動重建的原生方式更新。

設置自動更新檢查頻率 

默認情況下 Watch­tower 每 5 分鐘會輪詢一次,如果你覺得這個頻率太高了可以使用如下選項來控制更新檢查的頻率,但二者只能選擇其一。

--interval, -i - 設置更新檢測時間間隔,單位為秒。比如每隔 1 個小時檢查一次更新: 

  1. docker run -d \  
  2.     --name watchtower \  
  3.     --restart unless-stopped \  
  4.     -v /var/run/docker.sock:/var/run/docker.sock \  
  5.     containrrr/watchtower -c \  
  6.     --interval 3600 

--schedule, -s - 設置定時檢測更新時間。格式為 6 字段 Cron 表達式,而非傳統的 5 字段,即第一位是秒。比如每天凌晨 2 點檢查一次更新: 

  1. docker run -d \  
  2.     --name watchtower \  
  3.     --restart unless-stopped \  
  4.     -v /var/run/docker.sock:/var/run/docker.sock \  
  5.     containrrr/watchtower -c \  
  6.     --schedule "0 0 2 * * *" 

手動更新

前面的使用方式都是讓 Watch­tower 以 detached(后臺)模式在運行并自動更新容器,而 Watch­tower 也支持以 foreground(前臺)模式來使用,即運行一次退出并刪掉容器,來實現手動更新容器。這對于偶爾更新一次那些不在自動更新列表中的容器非常有用。

對于 foreground 模式,需要加上 --run-once 這個專用的選項。下面的例子 Docker 會運行一次 Watch­tower 并檢查 aria2-pro 容器的基礎鏡像更新,最后刪掉本次運行創建的 Watch­tower 容器。 

  1. docker run --rm \  
  2.     -v /var/run/docker.sock:/var/run/docker.sock \  
  3.     containrrr/watchtower -c \  
  4.     --run-once \  
  5.     aria2-pro 

--run-once 可以簡寫為 -R: 

  1. docker run --rm \  
  2.     -v /var/run/docker.sock:/var/run/docker.sock \  
  3.     containrrr/watchtower -cR \  
  4.     aria2-pro 

需要注意的是當這個容器設置過 com.centurylinklabs.watchtower.enable=false 參數時不會更新。

尾巴

以上是博主在使用 Watch­tower 中總結的一些使用方式和方法,當然它還有一些其它的功能與使用方式,比如電子郵件通知、監視私人注冊表的鏡像、更新遠程主機上的容器等,這些對于一般用戶來說可能很少會用到,所以這里就不贅述了,感興趣的小伙伴可以去研究 Watchtower 官方文檔。 

 

責任編輯:龐桂玉 來源: 奇妙的Linux世界
相關推薦

2022-08-14 19:33:24

Watchtower開源Docker

2019-02-25 10:18:43

工具代碼測試

2020-08-28 10:40:13

PythonFaker數據

2022-10-09 10:11:30

Python爬蟲神器

2024-01-25 10:40:11

AutoProfil開源分析工具

2021-04-27 09:00:59

PythonAidLearning編程神器

2020-04-30 10:45:14

IDEA代碼神器工具

2024-03-26 12:22:03

Visio軟件

2022-04-20 09:26:08

Mock前端開發工具

2025-04-07 08:10:00

2014-01-13 15:00:51

InxiLinux硬件

2024-08-22 12:35:37

2023-09-06 08:19:53

2015-03-30 14:15:55

自動更新Android

2020-02-17 07:20:22

SSH遠程連接工具Linux

2020-10-14 11:05:10

Java開發IDEA

2020-12-15 15:08:17

工具Java線程

2020-12-15 07:54:40

工具Hutoolgithub

2024-12-27 12:10:58

2023-06-08 08:46:37

Motrix下載工具
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日本黄色片免费在线观看 | 亚洲成人久久久 | 欧美日韩精品 | 九九热在线免费观看 | 日韩三级视频 | 一区二区久久 | 一区二区视频在线观看 | 一级毛片在线播放 | 99一区二区| 欧美久久一级 | 天天操人人干 | 中文字幕精品一区二区三区精品 | 欧美一区二区在线 | 欧美高清视频一区 | 精品国产一区一区二区三亚瑟 | 天天草天天干 | 精品丝袜在线 | 欧美.com| 四虎影院在线观看av | 91色视频在线 | 超碰免费在线 | www.四虎.com | 日韩中文字幕免费在线观看 | 国产999精品久久久 精品三级在线观看 | 日韩av黄色| 久久一| 一区二区三区欧美 | 国产成人网 | 精品视频久久久久久 | 国产欧美一区二区三区在线看蜜臀 | 精品国产久 | 欧美激情一区 | 国产精品久久久久aaaa九色 | 久久9热 | 国产精品亚洲一区二区三区在线观看 | 亚洲精品888 | 日韩一级二级片 | 精品国产99 | 91免费看片| 欧美国产一区二区 | 99精品一区二区三区 |