Skip to main content

Watchtower

th-822873674.jpeg

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. You need to run an instance of Watchtower on each server where you run Docker.Follow these steps to get Watchtower up and running:

    Make a directory for your Watchtower project and then navigate into it:
    mkdir ~/watchtower
    cd ~/watchtower
      Create a new YAML file named docker-compose.yml using nano or your preferred text editor:
      nano docker-compose.yml
        Insert the following into docker-compose.yml:
        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. 

        Make sure to put a "space" between the names of the images you want to monitor

          Save and exit your file. If you used nano, you can do this by pressingCTRL+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:
          docker compose up -d

          Passing a list of containers to monitor, which does not include the watchtower container, will disable the monitoring of watchtower. By adding it to the argument list, it will start automatically updating itself.