[小技巧] 自动更新和手动更新docker容器

date
Nov 8, 2024
slug
tip-container-update-auto-or-manual
status
Published
summary
小技巧
tags
tip
type
Post
URL

Problem & Summary

    自动

    # auto
    docker run -d \
        --name watchtower \
        -v /var/run/docker.sock:/var/run/docker.sock \
        containrrr/watchtower
        
    # or
    cat > docker-compose.yml <<EOF
    version: '3'
    services:
      watchtower:
        image: containrrr/watchtower
        volumes:
          - /var/run/docker.sock:/var/run/docker.sock
        restart: unless-stopped
        # -c: cleanup
        # run every day at 2am
        # Only checking containers using enable label
        command: ["--cleanup", "--label-enable", "--schedule", "0 0 2 * * *"]
        #command: ["--cleanup", "--label-enable"]
    EOF
    
    docker compose down; docker compose up -d; sleep 1s; docker compose logs -f
    
    # 完成之后,再运行其他的 docker 容器时,添加
    # --label 
    #   com.centurylinklabs.watchtower.enable=true 
    # 即可,添加了这个标签的容器会被 watchtower 监控并自动更新,docker-compose 格式如下:
    
    version: '3'
    services:
      myapp:
        image: your-image:latest
        labels:
          - com.centurylinklabs.watchtower.enable=true
        volumes:
          - /var/run/docker.sock:/var/run/docker.sock
        restart: always
        # 其他容器配置...
    
    
    
    # 这个则会被永久忽略更新
    #     labels:
    #      - com.centurylinklabs.watchtower.enable=false
    notion image

    手动

    docker pull docker-name
    docker restart docker-name
    
    # or
    
    docker-compose  pull
    docker compose down; docker compose up -d; sleep 1s; docker compose logs -f
     

    © Ying Bun 2021 - 2024