Servers

Setting up Watchtower for Docker

Watchtower for Docker

Setting up Watchtower for Docker

Background Info

What is Docker?

Docker is really a thing these days.
In a nutsheel, it allows running web applications (like WordPress) in containers which work similar to Virtual Machines.
Or in Wikipedia words:

Docker is a set of platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels. All containers are run by a single operating-system kernel and are thus more lightweight than virtual machines.

I decided to give it a try and I was stunned by the simplicity and the ease of use.
Along with Docker I installed Portainer which gives a nice user interface for managing the docker containers.

What’s the issue?

Portainer gives the option to update containers with new images and versions.
The only problem is to upgrade Portainer itself, you can’t do it unless you use the Docker CLI and using CLI isn’t fun for most of the people.

Watchtower

Watchtower comes to the rescue and it is a tool that runs as a container under Docker and check for new image updates for the containers you choose.
When it detects a change, it’ll pull the new image and recreate the container based on the updated image.
Brilliant!

Let’s do it

Things to consider before you start

  1. ALWAYS create backups for the content you need before you proceed with updating / upgrading or changing.
  2. Decide which container you want to keep updated, usually it’s not necessary to update all of them.
  3. Check if your container’s volume is mounted on the host, if the config is on the container it’ll be lost when updated.

Setting up Watchtower

First login through SSH to your server.
Create new folder, I named it “Watchtower” and CD into the folder.
Use your favorite text editor to create new file called docker-compose.yml

Paste this code inside:

version: '3'
services:
   watchtower:
      container_name: watchtower
      restart: always
      image: containrrr/watchtower
      environment:
      - WATCHTOWER_NOTIFICATIONS=email
      - [email protected]
      - [email protected]
      - WATCHTOWER_NOTIFICATION_EMAIL_SERVER=youremail.server
      - [email protected]
      - WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD=yourpassword
      - WATCHTOWER_NOTIFICATION_EMAIL_DELAY=2
      - TZ=yourtimezone
      volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      command: --debug true --interval 1800 --cleanup true portainer

Change the settings you need, like the email server and username etc.
If you don’t want to use the email notifications you can delete all those lines.
You can set the timezone to adjust the time on the logs automatically (timezones can be found here)

The command parameters:
— debug Will show info on the logs.
— interval Sets the interval of checking the images (in seconds).
— cleanup Will delete the old images to clear some space.

Save the file and run:
docker-compose up -d
(You might need to use SUDO)

Now you can go to Portainer and check the logs of the new container called Watchtower.

portainer watchtower log
As you can see, it’s checking every few minutes, later on I changed it to 30 min.

Hope it was useful.

73!

Did you like it?

Leave a Comment

Your email address will not be published. Required fields are marked *

Skip to content