# Watchtower

![th-822873674.jpeg](https://bookstack.timshome.net/uploads/images/gallery/2023-03/scaled-1680-/r5IiSSKSEMVXNO0o-th-822873674.jpeg)

[**Watchtower** ](https://containrrr.dev/watchtower/)is an application that will monitor your running Docker containers and watch for changes to the images that those containers were originally started from. If watchtower detects that an image has changed, it will automatically restart the container using the new image.

With watchtower you can update the running version of your containerized app simply by pushing a new image to the Docker Hub or your own image registry. Watchtower will pull down your new image, gracefully shut down your existing container and restart it with the same options that were used when it was deployed initially.

---

#### Installing Watchtower

I am using Docker Compose to run my Watchtower instances.

<p class="callout info">You need to run an instance of Watchtower on each server where you run Docker containers.</p>

**Follow these steps to get Watchtower up and running:**

- Make a directory for your Watchtower project and then navigate into it:

```shell
mkdir ~/watchtower
cd ~/watchtower
```

- Create a new YAML file named `docker-compose.yml` using `nano` or your preferred text editor:

```shell
nano docker-compose.yml
```

- Insert the following into `docker-compose.yml`:

```yaml
version: "3"
services:
  watchtower:
    container_name: watchtower
    image: containrrr/watchtower
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    restart: unless-stopped
    environment:
      - TZ=America/New_York
      - WATCHTOWER_LIFECYCLE_HOOKS=1 # Enable pre/post-update scripts
    command: --debug true --cleanup true dockerimage1 dockerimage2 dockerimage3
```

Where "dockerimage" 1, 2, and 3 are the names of the docker images I want to monitor and update when a change occurs to the original image.

<p class="callout warning">Make sure to put a "space" between the names of the images you want to monitor</p>

- Save and exit your file. If you used `nano`, you can do this by pressing`CTRL+O`, `ENTER`, then `CTRL+X`. Now you can start your containers using `docker compose up`. Add the `-d` flag to prevent Docker from taking over your terminal:

```shell
docker compose up -d
```

<p class="callout info">Passing a list of containers to monitor, which does <span style="text-decoration: underline;">not</span> include the watchtower container, will disable the monitoring of watchtower. By adding it to the argument list, it will start automatically updating itself. </p>