====== 使用 watchtower 自動更新 docker images ====== ==== 定期模式 ==== * .env 檔加入 THE_HOST echo THE_HOST=`hostname` >> .env * 在 docker-compose.yml 內 services: 加入 watchtower: Exp services: your-app: image: your-image:latest # 其他應用配置... watchtower: container_name: watchtower image: containrrr/watchtower volumes: - /var/run/docker.sock:/var/run/docker.sock environment: - TZ=Asia/Taipei - WATCHTOWER_SCHEDULE=0 0 4 * * * # 每天凌晨4點檢查更新 - WATCHTOWER_CLEANUP=true # 清理舊的映像檔 #- WATCHTOWER_NOTIFICATIONS=slack # 使用 Slack 通知 #- SLACK_HOOK_URL=https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK #- WATCHTOWER_NOTIFICATIONS=email # 使用 EMail 通知 #- WATCHTOWER_NOTIFICATIONS_HOSTNAME=${THE_HOST} #- WATCHTOWER_NOTIFICATION_EMAIL_FROM=fromaddress@gmail.com #- WATCHTOWER_NOTIFICATION_EMAIL_TO=toaddress@gmail.com #- WATCHTOWER_NOTIFICATION_EMAIL_SERVER=smtp.gmail.com #- WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PORT=587 #- WATCHTOWER_NOTIFICATION_EMAIL_SERVER_USER=fromaddress@gmail.com #- WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD=app_password #- WATCHTOWER_NOTIFICATION_EMAIL_DELAY=2 labels: - "com.centurylinklabs.watchtower.enable=true" restart: unless-stopped ==== CLI 模式 ==== * 直接執行 docker cli docker run --rm -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower --run-once 或是 docker compose run watchtower --run-once ==== API 模式 ==== * 參考 - https://containrrr.dev/watchtower/http-api-mode/ * 如果不想定期檢查更新, 可以透過呼叫 API 方式進行檢查更新 * Exp. Listen Port:8383 , API-Token:mytoken-123456789 * docker-compose.yml services: watchtower: container_name: watchtower image: containrrr/watchtower volumes: - /var/run/docker.sock:/var/run/docker.sock command: --http-api-update environment: - TZ=Asia/Taipei - WATCHTOWER_HTTP_API_TOKEN=mytoken-123456789 - WATCHTOWER_CLEANUP=true - WATCHTOWER_NOTIFICATIONS=email - WATCHTOWER_NOTIFICATIONS_HOSTNAME=${THE_HOST} - WATCHTOWER_NOTIFICATIONS_HOSTNAME=myserver - WATCHTOWER_NOTIFICATION_EMAIL_FROM=tryweb@ichiayi.com - WATCHTOWER_NOTIFICATION_EMAIL_TO=tryweb@ichiayi.com - WATCHTOWER_NOTIFICATION_EMAIL_SERVER=smtp.gmail.com - WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PORT=587 - WATCHTOWER_NOTIFICATION_EMAIL_SERVER_USER=myaccountid - WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD=mypassword - WATCHTOWER_NOTIFICATION_EMAIL_DELAY=2 labels: - "com.centurylinklabs.watchtower.enable=true" ports: - 8383:8080 restart: unless-stopped * 呼叫 API 方式 curl -H "Authorization: Bearer mytoken-123456789" http://localhost:8383/v1/update ===== 參考網址 ===== * https://claude.ai/ * https://containrrr.dev/watchtower/ {{tag>docker auto-upgrade}}