Unattended updates using Watchtower

· cenotaph's docs

This guide details how to set up unattended, automatic updates of your docker containers using Watchtower.

Introduction

A regular problem with docker instances that are very "set and forget" is remembering to update all your images regularly, especially if you have a lot of them. Watchtower is a container that solves this for you by automatically checking for updates and updating containers that you specify on a schedule of your choosing.

Note: if you are using Gluetun you will want to exclude this from any watchtower config as this will always break your setup and require manual fix which is inconvenient if you are not available to fix it. To prevent this, either a) don't include Gluetun when making the list of containers to update or b) add the following property to your Gluetun compose file:

    labels:
      - com.centurylinklabs.watchtower.enable=false

This will exclude the Gluetun container when having watchtower set to "update everything".


Your Docker compose

Thankfully, setting up watchtower is incredibly simple. The docker compose is as follows:
services:
  watchtower:
    image: containrrr/watchtower:latest
    container_name: watchtower
    environment:
      - TZ=Your/TZ
      - WATCHTOWER_CLEANUP=true
      - WATCHTOWER_INCLUDE_STOPPED=true
      - WATCHTOWER_REVIVE_STOPPED=false
      - WATCHTOWER_SCHEDULE=0 0 4 * * *
    command:
      # add or remove the below as required
      - container1
      - container2
      - container3
      - container4
      - container5
      - container6
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    restart: unless-stopped

Now, let's break down what each piece of the docker-compose does.

Next there are a number of environment variables set. I'll address them in order:

Next up is actually an optional section. If you would like watchtower to upgrade every container you have, you can simply ignore the entire "command:" section. Not having the command section will make it update any and all containers that you have not explicitly excluded with the tag I listed above for excluding Gluetun. I personally like to have it only update the containers I explicitly state, so I include it. Below the command section you simply need to list the "container_name:" variable of any containers you would like to see updated. If you aren't sure about what name to use for a particular container, you can see all running containers along with their name identifiers by running docker ps -s in the terminal of the machine you are running docker on.

Nearing the bottom, we have our volume. All this does is binds the /var/run/docker.sock in the container-space to the actual /var/run/docker.sock on your machine. The left side of the colon is your machine, the right side of the colon is the directory on the container.

Lastly, restart: unless-stopped ensures that watchtower will restart itself if it crashes, unless you have manually stopped it.

After you have set all of these variables, you can spin up your docker container using the gui of your choice or by using docker compose run watchtower. After that, all you have to do is wait for your specified schedule to trigger and then see the logs for the container. It will tell you when it made the check, how many containers were updated, and you'll be able to see that it is working.

Have questions? Ask in one of the Signal groups or contact me at cenotaph.contact@pm.me

last updated: