How to Automatically Update Docker Containers with Watchtower

Learn how to set up Watchtower to automatically update your Docker containers.

Publish date: 6/3/2024

Keeping your Docker containers updated can be a hassle, especially if you manage multiple containers.

This is where Watchtower comes into play.

Watchtower is an open-source tool that automatically updates your Docker containers whenever a new image is available, and in this article, we'll be showing you how to set it up.

Let’s dive in.

How to set up Watchtower

Prerequisites

  • A system with Docker and Docker Compose installed. Linux, Windows, and Mac OS all work with Docker, but we tend to recommend using Linux. Our favorite distribution is Debian 12 (by the way, you can get an affordable Linux VPS for running Docker apps over at V.PS, our VPS hosting brand, if you're in need).
  • Basic knowledge of Docker commands (we just published a guide that explains how to set up Docker and Docker Compose on Debian 12 right here, it'll help with Ubuntu as well, and other Linux distributions will only be slightly different).

Step 1) Install Watchtower

First, you need to pull the Watchtower image from Docker Hub.

Open your terminal and run the following command:

docker pull containrrr/watchtower

Step 2) Run Watchtower

To start Watchtower, you need to run a Docker container using the Watchtower image.

The command below will do the trick:

docker run -d \
  --name watchtower \
  -v /var/run/docker.sock:/var/run/docker.sock \
  containrrr/watchtower

This command runs Watchtower in detached mode (-d), names the container watchtower, and mounts the Docker socket so Watchtower can communicate with the Docker daemon.

Step 3) Configure Watchtower

Watchtower has various configuration options. You can customize its behavior by passing environment variables or command-line arguments.

Here’s an example of running Watchtower with a few additional options:

docker run -d \
  --name watchtower \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -e WATCHTOWER_CLEANUP=true \
  -e WATCHTOWER_POLL_INTERVAL=300 \
  containrrr/watchtower
  • WATCHTOWER_CLEANUP=true instructs Watchtower to remove old images after updating.
  • WATCHTOWER_POLL_INTERVAL=300 sets the interval for checking updates to every 5 minutes (300 seconds).

Watchtower also supports more advanced configurations such as monitoring specific containers only, sending notifications to Slack or email, and more. These features can be explored in detail in the Watchtower documentation.

Step 4) Verify Watchtower

To ensure Watchtower is running correctly, you can check its logs:

docker logs watchtower

If everything is set up properly, you'll see logs indicating that Watchtower is checking for updates and managing your Docker containers.

Such as this:

root@test:~# docker logs watchtower
time="2024-06-02T05:45:43Z" level=info msg="Watchtower 1.7.1"
time="2024-06-02T05:45:43Z" level=info msg="Using no notifications"
time="2024-06-02T05:45:43Z" level=info msg="Checking all containers (except explicitly disabled with label)"
time="2024-06-02T05:45:43Z" level=info msg="Scheduling first run: 2024-06-02 05:50:43 +0000 UTC"
time="2024-06-02T05:45:43Z" level=info msg="Note that the first check will be performed in 4 minutes, 59 seconds"
time="2024-06-02T05:50:44Z" level=info msg="Session done" Failed=0 Scanned=2 Updated=0 notify=no
time="2024-06-02T05:52:24Z" level=info msg="Waiting for running update to be finished..."

Using Watchtower with Docker Compose

Having established the process for using Watchtower with a single Docker container, let's talk about using Docker Compose.

Docker Compose allows you to define and run multiple Docker containers with a single YAML file. This is particularly useful for those self-hosting many apps, as it simplifies the management of multi-container applications.

Docker Compose in combination with Watchtower makes it very easy to maintain both development and production environments.

Setting up Watchtower with Docker Compose

To integrate Watchtower into your Docker Compose setup, you can add it as a service in your docker-compose.yml file.

Here’s an example configuration:

services:
  web:
    image: nginx:alpine
    ports:
      - "80:80"

  watchtower:
    image: containrrr/watchtower
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - WATCHTOWER_CLEANUP=true
      - WATCHTOWER_POLL_INTERVAL=300

Running the Docker Compose setup

To start the services defined in the docker-compose.yml file, use the following command:

docker compose up -d

That will start both the Nginx web server and the Watchtower container.

If you go to your server's public IP address via browser, you should see Nginx running. You can also type docker ps to see the active containers, and you can use docker logs again to verify Watchtower is actively running and waiting to update your containers per your designated interval.

Congrats! You now know how to automatically keep all of your Docker containers up to date.

Conclusion

If you're looking for an easy way to keep your Docker containers updated; look no further than Watchtower.

It's a system administrator or software engineer's best friend when it comes to simplifying your deployment process and workflow -- even if it's for hobbyist use like keeping your Plex server up to date automatically!

Give it a try, and I'm willing to bet you won't regret it.

As always, if you need reliable hosting solutions, xTom offers premium quality services for both large-scale production and smaller projects. Check out xTom.com for enterprise solutions or our NVMe V.PS line for scalable, personal projects.

Happy Dockering, and thanks for reading!