Watchtower with docker-compose

What is Watchtower?

Watchtower is a container-based solution that automates base image updates for Docker containers.

Its main purpose is to make it easier for containerized programs to update without interruption as they run by retrieving a fresh image from your own image registry or Docker Hub.

It gracefully stops the current container when a new image is pushed, takes down the updated image, and restarts it with the same configurations as the first deployment.

Watchtower is simple to integrate into Docker-based setups since it uses the Docker Compose file to run the Watchtower container.

You may take advantage of seamless deployment, automated upgrades, simplified maintenance, adaptability in image sources, and integration with Docker for an all-inclusive container management solution when you use Watchtower with Docker.

Getting Started

Requirements

You need to have docker installed on your computer. If you don't have it jet follow the instructions from the docker docs.

I would recoment to firstly run Gotify to have a gui for the Notifications from Watchtower.
In the last post you find a instruction to run Gotify behind Traefik with docker-compose.

If you prefere to run it without Gotify you can skip the following optional parts.

 

Create a token in Gotify ( optional )

In the Gotify webinterface you can create new applications to get a token for your watchtower notifications.

Create your docker-compose.yml

## docker-compose.yml
version: '3.8'
# network is optional just needed with gotify
networks:
 default:
   name: "traefik_net"
   external: true

services:
 watchtower:
   image: “containrrr/watchtower”
   container_name: “demo_watchtower”
   hostname: “watchtower.localhost”
   restart: “unless-stopped”
   environment:
     # Following options are otrional START
     - “WATCHTOWER_NOTIFICATIONS=gotify”
     - “WATCHTOWER_NOTIFICATION_GOTIFY_URL=http://gotify.localhost”
     - “WATCHTOWER_NOTIFICATION_GOTIFY_TOKEN=YOUR_GOTIFY_TOKEN”
     # END
     - “WATCHTOWER_CLEANUP=true”
     - “WATCHTOWER_LABEL_ENABLE=true”
     - “WATCHTOWER_INCLUDE_RESTARTING=true”
     - “WATCHTOWER_ROLLING_RESTARTING=true”
     - “WATCHTOWER_INTERVAL=200”
     - “WATCHTOWER_INCLUDE_STOPPED=true”
   security_opt:
     - “no-new-privileges:true”
   volumes:
     - “/etc/timezone:/etc/timezone:ro”
     - “/etc/localtime:/etc/localtime:ro”
     - “/var/run/docker.sock:/var/run/docker.sock”
   labels:
     # Watchtower add to auto update
     - “com.centurylinklabs.watchtower.enable=true”
     # traefik labels are optional ( just with gotify )
     - “traefikf.enable=true”
     - “traefik.docker.network=traefik_net”
     - “traefik.http.routers.${PROJECT_NAME}_watchtower.entrypoints=websecure”
     - “traefik.http.routers.${PROJECT_NAME}_watchtower.rule=Host(`watchtower.localhost`)”
     - “traefik.http.routers.${PROJECT_NAME}_watchtower.tls=true”
     - "traefik.http.services.${PROJECT_NAME}_watchtower.loadbalancer.server.port=3000"

In the above example every other local docker container with the following label and the latest tag will be updated every 200ms and give (optional) a notification to gotify.

     # Watchtower add to auto update
     - “com.centurylinklabs.watchtower.enable=true”

Start your docker-compse.yml

Now you can start your service with:

docker compose up -d

access gotify dashboard (optional)

Now you can access the gotify dashbord at https://gotify.localhost.
If you used the sample docker-compose form the previews post, you can login with:

User: admin
Password: password.1

to see the watchtower notifications.

you can find the example on my git server.