Docker Compose File For Watchtower
Category : Docker Compose Files
Watchtower is a docker container that will update the version of other running containers. You can change the environment settings as required for your installation – see the full documentation for more info.
version: '3' services: watchtower: image: containrrr/watchtower restart: always volumes: - /var/run/docker.sock:/var/run/docker.sock - /etc/timezone:/etc/timezone:ro environment: - WATCHTOWER_CLEANUP=true - WATCHTOWER_LABEL_ENABLE=true - WATCHTOWER_INCLUDE_RESTARTING=true labels: - "com.centurylinklabs.watchtower.enable=true"
Create a new directory and save the above file inside it as docker-compose.yml.
mkdir watchtower cd watchtower vi docker-compose.yml
Run docker-compose up -d to fetch the image from the docker hub and create your Watchtower instance. By default, Watchtower will check for updates every 24 hours from the moment you start the Watchtower container. You’ll now need to add labels to your docker containers that you’d like to update which is described below.
The above docker-compose file will get Watchtower up and running but in it’s current state it won’t update any of your existing docker containers. The above docker-compose file is safe by default so that it won’t do anything other than update itself – arguably useless, but safe!
To update your other docker containers you need to add a label to their docker-compose file that sets
com.centurylinklabs.watchtower.enable=true. Watchtower will then pick up that label and know that it needs to include that docker container (or service) in it’s updates.
The below is a random docker-compose file that I use to run a database for development work. I’ve added a labels attribute and set the parameter that Watchtower looks for when running it’s update routine. For your containers to be included in Watchtowers update routine you’ll need to add the same labels attribute to ALL of your docker-compose services, just like in the below example – its a simple copy and paste.
version: '3.6' services: db1: image: mysql:5.7 restart: unless-stopped volumes: - ./data/mysql:/var/lib/mysql - ./config/mysql/conf.d:/etc/mysql/conf.d environment: MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: db1 MYSQL_USER: db1 MYSQL_PASSWORD: password labels: - "com.centurylinklabs.watchtower.enable=true"
Once you’ve added the labels attribute, simply wait for a new image to be released and within 24 hours your container will have been updated, restarted and the old image removed (as long as it’s not used elsewhere).