Docker Updater : Watchtower

Watchtower 소개

Docker의 경우 기본적으로 자동 업데이트를 지원하지 않습니다.

만일 Docker Image가 업데이트 되어 버전을 올리고 싶다면 컨테이너 재구성 등의 매우 복잡한 과정을 거치셔야 합니다.

Watchtower를 이용하면 설정한 주기값으로 안전하게 Docker Image를 항상 최신 버전으로 자동 업데이트 하실 수 있습니다.

설치

"docker-compose.yml" 파일을 아래와 같이 생성 합니다.

version: "3"

services:
  watchtower:
    image: containrrr/watchtower
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      TZ: Asia/Seoul
      WATCHTOWER_POLL_INTERVAL: 43200
      # WATCHTOWER_SCHEDULE: "0 0 19 * * *"  # 4시 KST = 19시 전날 UTC
      WATCHTOWER_CLEANUP: "true"  # Enable cleanup of old images
    restart: unless-stopped

"WATCHTOWER_POLL_INTERVAL"은 watchtower가 실행될 주기를 의미합니다.
단위는 Second (초)이며, 예제에 있는 43200은 12시간을 의미 합니다.
특정 시간에 실행을 원하시면 "WATCHTOWER_SCHEDULE: "0 0 19 * * *" # 4시 KST = 19시 전날 UTC"를 사용하시면 됩니다.

docker를 실행 합니다.

docker-compose up -d

만일 특정 라벨 처리가 있는 것만 업데이트를 하려면 아래를 참고하세요.

version: "3"
services:
  watchtower:
    image: containrrr/watchtower
    container_name: watchtower
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - TZ=Asia/Seoul
    command: --label-enable --cleanup --interval 43200
    restart: unless-stopped

이 경우 아래와 같이 라벨이 적용된 컨테이너만 업데이트가 적용 됩니다.

labels: 
   com.centurylinklabs.watchtower.enable: "true"

이후 Watchtower Container의 Log를 보시면 아래와 같이 주어진 주기마다 Scan 및 Update를 진행한 이력을 확인하실 수 있습니다.

date stream content
2023/06/24 09:53:01 stderr time="2023-06-24T09:53:01+09:00" level=info msg="Session done" Failed=0 Scanned=9 Updated=0 notify=no
2023/06/23 09:53:01 stderr time="2023-06-23T09:53:01+09:00" level=info msg="Session done" Failed=0 Scanned=9 Updated=0 notify=no
2023/06/22 09:53:25 stderr time="2023-06-22T09:53:25+09:00" level=info msg="Session done" Failed=0 Scanned=9 Updated=1 notify=no
2023/06/22 09:53:24 stderr time="2023-06-22T09:53:24+09:00" level=info msg="Creating /nextcloud"
2023/06/22 09:53:19 stderr time="2023-06-22T09:53:19+09:00" level=info msg="Stopping /nextcloud (786e591f6e53) with SIGTERM"
2023/06/22 09:53:12 stderr time="2023-06-22T09:53:12+09:00" level=info msg="Found new lscr.io/linuxserver/nextcloud:latest image (3321996051d7)"
2023/06/21 09:52:45 stderr time="2023-06-21T09:52:45+09:00" level=info msg="Note that the first check will be performed in 23 hours, 59 minutes, 59 seconds"
2023/06/21 09:52:45 stderr time="2023-06-21T09:52:45+09:00" level=info msg="Scheduling first run: 2023-06-22 09:52:45 +0900 KST"
2023/06/21 09:52:45 stderr time="2023-06-21T09:52:45+09:00" level=info msg="Checking all containers (except explicitly disabled with label)"
2023/06/21 09:52:45 stderr time="2023-06-21T09:52:45+09:00" level=info msg="Using no notifications"
2023/06/21 09:52:45 stderr time="2023-06-21T09:52:45+09:00" level=info msg="Watchtower 1.5.3"
2023/06/21 09:41:05 stderr time="2023-06-21T09:41:05+09:00" level=info msg="Waiting for running update to be finished..."

예외처리

가끔 Watchtower를 이용해 docker 이미지가 Update된 이후 문제가 되는 docker들이 있습니다.

이 경우 Watchtower를 통한 Update를 원하지 않을 시 아래 단계를 진행하시면 됩니다.

Portainer를 통해 예외를 원하는 Container에 들어가서 "Duplicate/Edit" 를 선택 합니다.

exp1.png

아래쪽에 보면 "Labels"라는 메뉴가 있습니다.

exp2.png

"add label"을 선택 합니다.

exp3.png

"name"에 "com.centurylinklabs.watchtower.enable", "value"에 "false"를 입력 합니다.

exp4.png

라벨이 추가 됐으면 Container를 다시 빌드해야 합니다.

윗쪽 "Access control"에 있는 "Deploy the container"를 누르면 변경사항이 적용된 Container가 다시 빌드 됩니다.

exp5.png

이후 Watchtower Container의 Log를 보시면 아라와 같이 Label 처리가 안된 Continer만 Update 됨을 확인 하실 수 있습니다.

time="2023-06-21T09:52:45+09:00" level=info msg="Checking all containers (except explicitly disabled with label)"