使用 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} #- [email protected] #- [email protected] #- WATCHTOWER_NOTIFICATION_EMAIL_SERVER=smtp.gmail.com #- WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PORT=587 #- [email protected] #- WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD=app_password #- WATCHTOWER_NOTIFICATION_EMAIL_DELAY=2 labels: - "com.centurylinklabs.watchtower.enable=true" restart: unless-stopped
API 模式
- 如果不想定期檢查更新, 可以透過呼叫 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 - [email protected] - [email protected] - 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