BeTrfo

BeTrfo

love

Use WatchTower for automated container updates (simple usage)

With the popularity of Docker, basically 90% of projects will provide Docker for application deployment and isolation. Before deployment, it's fine if you have some basic experience and can set up how to update and save data in advance. However, for some users who are not familiar with Docker, they may only be able to copy and paste to run according to the user documentation, and they are quite confused about subsequent updates and can only redeploy once.
I accidentally discovered the project "WatchTower" on Github, and I think this might be the best solution for container updates.

Introduction#

Watchtower is a container management tool, and the recommended installation method is also to use Docker. It can monitor containers in Docker and automatically update containers and restore data after the images they depend on are updated.

Some advantages#

  1. Automatic updates, no manual intervention required, all data information will be saved during updates.
  2. Easy to use, the default running method is to directly run with Docker, no additional configuration is required.
  3. High degree of customization, supports configuring time points, image update strategies, container update strategies, etc.
  4. Lightweight and seamlessly integrates with existing services, no additional configuration is required.

Getting Started#

By default, WatchTower only needs to execute the following Docker command to start running. By default, it checks every five minutes, and the log will also include the time of each run and the estimated next run time.

docker run -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower

Here we can also add some other settings when starting.

  • Set the default time zone, here I directly set it to UTC+8.
  • Run in the background.
  • Set the restart policy, even if Docker restarts, it will continue to run.
# -d Run in the background
# -e TZ="Asia/Shanghai" Set the time zone to UTC+8.
# -v /var/run/docker.sock:/var/run/docker.sock Allow WatchTower to query container information.
docker run -d \
           --name watchtower \
           -e TZ="Asia/Shanghai" \
           --restart=always \
           -v /var/run/docker.sock:/var/run/docker.sock \
           containrrr/watchtower

Advanced Settings#

In addition to basic applications, here are some commonly used advanced configurations:

  • --run-once Only execute once.
  • --cleanup Clean up unused images after execution.
  • --include-stopped Also check stopped containers.
  • --schedule Execute using cron scheduling.
  • --interval Execute at intervals, choose either this or --schedule.

Combining the above, we can summarize some common ways:

  1. Manually execute an update of container images, including stopped containers, and perform cleanup after the update.
docker run --rm \
           --name watchtower \
           -e TZ="Asia/Shanghai" \
           -v /var/run/docker.sock:/var/run/docker.sock \
           containrrr/watchtower \
           --run-once \
           --cleanup \
           --include-stopped

image

  1. Run in the background and check every day at 5 AM (note that the cron used here has six digits, which is different from the default support of the Linux system).
docker run -d \
           --name watchtower \
           -e TZ="Asia/Shanghai" \
           -v /var/run/docker.sock:/var/run/docker.sock \
           containrrr/watchtower \
           --cleanup \
           --include-stopped \
           --schedule "0 0 5 * * *"

image

  1. Run in the background and check every hour (the unit here is s, so one hour is set to 3600).
docker run -d \
           --name watchtower \
           -e TZ="Asia/Shanghai" \
           -v /var/run/docker.sock:/var/run/docker.sock \
           containrrr/watchtower \
           --cleanup \
           --include-stopped \
           --interval 3600

image

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.