fbpx
Skip to content

Automatic control of Docker container updates with Watchtower

Watchtower is a tool for automatically updating Docker containers based on changes in the Docker Hub registry. It allows you to automatically update your containers in the background, eliminating the need to manually update each container manually. In this article, we’ll look at how to install Watchtower and an example of how to use it.

Watchtower installation

There are a few simple steps to install Watchtower:

  1. Install Docker on your server. You can use the official Docker documentation to install it on your operating system.
  2. Install Watchtower by running the following command:
docker run -d --name watchtower -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower

This command will download the Watchtower image from the Docker Hub and run it in the background. It will keep track of changes to your Docker Hub registry and update your containers automatically.

Using Watchtower

After installing Watchtower, you can use it to automatically update your containers. To do this, you need to start your container with the additional tag “latest”. For example, if you run a Nginx container, the start command would look like this:

docker run -d --name nginx --restart always -p 80:80 nginx:latest

Watchtower will keep track of the “latest” tag in the Docker Hub registry and update your container if it becomes obsolete.

You can also use Watchtower to update containers with other tags. To do this, you can specify a tag in the container start command, e.g:

docker run -d --name myapp --restart always myapp:1.0.0

Watchtower will keep track of the “myapp:1.0.0” image in the Docker Hub registry and update your container if it becomes obsolete.

Notification from Watchtower on Telegram

Watchtower allows you to send notifications to messengers when the container is updated. The full list of supported messengers is available on the official website. In this article we will figure out how to set up a notification for the Telegram messenger.

Creating a bot in Telegram

To receive notifications in Telegram from Watchtower, we will need to create a bot and get the ChatID of the channel to send notifications. Detailed information about creating a bot is available in the detailed instructions from Telegram.

Obtaining chat_id for a telegram channel

To send notifications to your telegram channel, you need to get the chat_id of the channel. To do this, add the bot to your channel and send any message to the channel. Then go to https://api.telegram.org/bot<YourBotToken>/getUpdates, replacing <YourBotToken> with your bot’s token. You will get a response from the Telegram API containing the chat_id of the channel. Copy it.

Setting up Watchtower

Now configure Watchtower to send notifications to your Telegram feed. To do this let’s add environment variables to the Watchtower setup command, which are responsible for configuring notifications

docker run -d --name watchtower \
  -e WATCHTOWER_LIFECYCLE_HOOKS=1 \
  -e WATCHTOWER_NOTIFICATIONS=shoutrrr \
  -e WATCHTOWER_NOTIFICATION_URL=telegram://<YourBotToken>@telegram/?channels=<YourChatId> \
  -v /var/run/docker.sock:/var/run/docker.sock \
  containrrr/watchtower

If you use docker-compose then the same code would look like this

version: '3.3'
services:
    watchtower:
        container_name: watchtower
        environment:
	    - WATCHTOWER_LIFECYCLE_HOOKS=1
            - WATCHTOWER_NOTIFICATIONS=shoutrrr
            - WATCHTOWER_NOTIFICATION_URL=telegram://<YourBotToken>@telegram/?channels=<YourChatId>
	command: --interval 30
        volumes:
            - '/var/run/docker.sock:/var/run/docker.sock'
        image: containrrr/watchtower

Replace <YourBotToken> and <YourChatId> with the corresponding values you got in the previous steps.

By default Watchtower checks containers at intervals of 24 hours, to reduce this interval to 30 seconds just add the command: –interval 30

Watchtower will now send notifications of the container update process to your Telegram channel. If everything is set up correctly, you should see notifications in your channel when containers are updated.

An example of a notification being sent

Found new portainer/portainer-ce:latest image (f031e549070f)
Found new amir20/dozzle:latest image (9602cf218ba7)
Found new traefik:latest image (d1e26b5f8193)
Stopping /traefik (a262f7327b90) with SIGTERM
Stopping /dozzle (27f1f5fd4110) with SIGTERM
Stopping /portainer (3ddfa13bb8fc) with SIGTERM
Creating /portainer
Creating /dozzle
Creating /traefik

In conclusion, Watchtower is a simple and convenient tool for automatically updating your Docker containers. It avoids the need to manually update each container and keeps your infrastructure up to date. Installing and using Watchtower takes only a few minutes and makes your Docker experience much easier.