Reading time: 3 minutes
Hello, today we are going to see how we can automatically update our Docker containers using Watchtower.
Step 1: Installation of Docker and Docker Compose Before we begin, make sure you have Docker and Docker Compose installed on your system. You can follow the official Docker instructions to install these tools on your operating system.
Step 2: Create a Docker Compose configuration file Create a file called docker-compose.yml
in the root directory of your project. This file will contain the configuration of your containers, including Watchtower.
Here is a basic example of a docker-compose.yml
file:
version: "3" services: myapp: image: myapp:latest restart: always watchtower: image: containrrr/watchtower volumes: - /var/run/docker.sock:/var/run/docker.sock restart: always command: --interval 30
In this example, there are two defined services: myapp
and watchtower
. The myapp
service is the container you want to update, and the watchtower
service is responsible for monitoring and updating the myapp
container.
The /var/run/docker.sock
volume is mounted in the Watchtower container to allow communication with the Docker daemon on the host.
Step 3: Additional Watchtower Configuration You can customize Watchtower’s configuration by adding more command-line options to the service in the docker-compose.yml
file. Here are some examples of common configurations:
- Update only specific containers: If you have multiple containers in your project and you only want Watchtower to update a particular one, you can add the
com.centurylinklabs.watchtower.enable=true
label to the container you want to update. For example:
version: "3" services: myapp: image: myapp:latest restart: always labels: - com.centurylinklabs.watchtower.enable=true watchtower: image: containrrr/watchtower volumes: - /var/run/docker.sock:/var/run/docker.sock restart: always command: --interval 30
Schedule updates: You can configure Watchtower to update containers at specific times using the --schedule
option. For example, to schedule a daily update at 3 a.m., you can modify the Watchtower command as follows:
watchtower: image: containrrr/watchtower volumes: - /var/run/docker.sock:/var/run/docker.sock restart: always command: --interval 30 --schedule "0 3 * * *"
Step 4: Watchtower Notification Configuration
To receive notifications when Watchtower updates a container, you can follow these steps:
- Add the following line to the Watchtower service in your
docker-compose.yml
file:
watchtower: image: containrrr/watchtower volumes: - /var/run/docker.sock:/var/run/docker.sock restart: always command: --interval 30 --notifications <notification>
Replace <notification>
with the type of notification you want to configure. Some examples of notification types are email
, slack
, gotify
, discord
, among others. The choice depends on your preferences and the notification service you use.
Configure the specific notification parameters according to the selected type. For example, if you choose to send email notifications
for email, add the --notification-email
parameter to the Watchtower service. Make sure to provide the correct email address.
watchtower: image: containrrr/watchtower volumes: - /var/run/docker.sock:/var/run/docker.sock restart: always command: --interval 30 --notifications email --notification-email your_email@example.com
Save the docker-compose.yml
file.
We can also generate a notification using Discord:
Add the following line to the Watchtower service in your docker-compose.yml
file:
watchtower: image: containrrr/watchtower volumes: - /var/run/docker.sock:/var/run/docker.sock restart: always command: --interval 30 --notifications discord
Configure the specific notification parameters for Discord. Add the following parameters to the Watchtower service:
watchtower: image: containrrr/watchtower volumes: - /var/run/docker.sock:/var/run/docker.sock restart: always environment: WATCHTOWER_NOTIFICATIONS: slack WATCHTOWER_NOTIFICATION_SLACK_HOOK_URL: <discord_webhook>/slack WATCHTOWER_NOTIFICATION_TEMPLATE: {{- if .Report -}} {{- with .Report -}} {{len .Scanned}} Scanned, {{len .Updated}} Updated, {{len .Failed}} Failed {{- range .Updated}} - {{.Name}} ({{.ImageName}}): {{.CurrentImageID.ShortID}} updated to {{.LatestImageID.ShortID}} {{- end -}} {{- range .Fresh}} - {{.Name}} ({{.ImageName}}): {{.State}} {{- end -}} {{- range .Skipped}} - {{.Name}} ({{.ImageName}}): {{.State}}: {{.Error}} {{- end -}} {{- range .Failed}} - {{.Name}} ({{.ImageName}}): {{.State}}: {{.Error}} {{- end -}} {{- end -}} {{- else -}} {{range .Entries -}}{{.Message}}{{"\n"}}{{- end -}} {{- end -}} command: notify-upgrade
Replace <DISCORD_WEBHOOK_URL>
with the URL of the Discord webhook you want to send notifications to. To obtain the webhook URL, you need to create a webhook on your Discord server.
Save the docker-compose.yml
file.
Step 5: Run the Tutorial
Once you have configured the docker-compose.yml
file with the desired Watchtower options and notifications, you can run the tutorial by following these steps:
- Open a terminal and navigate to the directory where the
docker-compose.yml
file is located. - Run the following command to start the containers:
docker-compose up -d
This will create and run the containers defined in the docker-compose.yml
file.
Watchtower will now monitor your containers and update them according to the configured settings. If updates are available, you will receive notifications based on the notification configuration you have defined.