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 the containers in Docker and automatically update the containers and restore data after the images they depend on are updated.
Some advantages#
- Automatic updates, no manual intervention is required, and all data information will be saved during updates.
- Easy to use, the default running method is to run directly with Docker without any additional configuration.
- High degree of customization, supports configuring time points, image update strategies, container update strategies, etc.
- Lightweight and seamlessly integrated 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 GMT+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 GMT+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 extended 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, some common methods can be summarized.
- Manually execute an update of container images, including stopped containers, and perform cleanup after updating.
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
- Run in the background and perform a check every day at 5 AM (note that the
cron
used here is six digits, which is different from what the Linux system supports by default).
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 * * *"
- Run in the background and perform a check every hour (the unit here is
s
, so one hour is set to3600
).
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