Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Watchtower to Automatically Update Your Docker Containers Using Docker Compose

Tiempo de lectura: 3 minutos

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
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
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:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
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
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
watchtower:
image: containrrr/watchtower
volumes:
- /var/run/docker.sock:/var/run/docker.sock
restart: always
command: --interval 30 --schedule "0 3 * * *"
watchtower: image: containrrr/watchtower volumes: - /var/run/docker.sock:/var/run/docker.sock restart: always command: --interval 30 --schedule "0 3 * * *"
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:

  1. Add the following line to the Watchtower service in your docker-compose.yml file:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
watchtower:
image: containrrr/watchtower
volumes:
- /var/run/docker.sock:/var/run/docker.sock
restart: always
command: --interval 30 --notifications <notification>
watchtower: image: containrrr/watchtower volumes: - /var/run/docker.sock:/var/run/docker.sock restart: always command: --interval 30 --notifications <notification>
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
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
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
watchtower:
image: containrrr/watchtower
volumes:
- /var/run/docker.sock:/var/run/docker.sock
restart: always
command: --interval 30 --notifications discord
watchtower: image: containrrr/watchtower volumes: - /var/run/docker.sock:/var/run/docker.sock restart: always command: --interval 30 --notifications discord
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
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
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:

  1. Open a terminal and navigate to the directory where the docker-compose.yml file is located.
  2. Run the following command to start the containers:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker-compose up -d
docker-compose up -d
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.

0

Leave a Comment