How To Automatically Update Running Docker Containers

Watchtower - Automatically Update Running Docker Containers

Watchtower is a free, open source application that allows you to monitor the running Docker containers and updates them automatically if it finds any changes in their base images. When watchtower finds that a running container needs to be updated, it will gracefully stop the running container by sending it a SIGTERM signal. It will then download the new image, and finally restart the Container with the same options that were used when it was deployed initially. Everything will be done automatically on the background, so the user intervention is not required. In this guide, we will see how to automatically update running Docker containers using Watchtower in Unix-like operating systems.

I tested this guide in CentOS 7 minimal edition, however the procedure is same for all Linux distributions.

Watchtower – Automatically Update Running Docker Containers

Install Watchtower

Watchtower itself is available as Docker image. So, deploying it is not a big deal. Install Docker on your Linux box, and start running Watchtower to monitor the Docker containers in no time.

Refer the following guides to install Docker on YUM based and DEB based systems.

Once Docker installed, you can deploy the Watchtower container using the following command as root user:

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

This command will pull the latest image of watchtower, and start watchtower container.

Sample output:

Unable to find image 'v2tec/watchtower:latest' locally
latest: Pulling from v2tec/watchtower
a3ed95caeb02: Pull complete 
802d894958a2: Pull complete 
9916c27d2815: Pull complete 
Digest: sha256:d89ea18b6332cc83639771fda274ebfd76f7042856d8f4997e99c7925ad02ae9
Status: Downloaded newer image for v2tec/watchtower:latest
e85c923f2e4dd22a62c12b038ea9694ba9245dcd835f210e1a1f902ee311e004

Usage

Watchtower has now started with other running containers on your system. You can view the list of running Docker containers using command:

docker ps

Sample output:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e85c923f2e4d v2tec/watchtower "/watchtower" 3 minutes ago Up 3 minutes watchtower
58543e6ed18a centos:latest "/bin/bash" 5 minutes ago Up 5 minutes ostechnix

As you see in the above output, Watchtower container is running along with another container named “ostechnix”. From now on, Watchtower will start watching this container every few minutes. If it finds any changes in the this container’s base image, it will gracefully shutdown the “ostechnix” container, and restart it with new image with same options that were used when it was started initially. Similarly, it will automatically check for updates for all running containers every few minutes, and updates them automatically.

By default, Watchtower will monitor all Docker containers running within the Docker daemon to which it is pointed. However, you can limit watchtower to monitor a particular Docker container by specifying the container’s name as shown below.

docker run -d --name watchtower -v /var/run/docker.sock:/var/run/docker.sock v2tec/watchtower ostechnix

In the above example, watchtower will only monitor the container named “ostechnix” for updates, and other running containers will be ignored. If you don’t specify any arguments, then watchtower will monitor all running Docker Containers as usual.


Recommended Read: Portainer – An Easiest Way To Manage Docker


Watchtower is smart enough when it comes to monitoring multiple linked containers. Let us say you’re running two containers named “Nginx” and “wordpress” container which is linked to “Nginx” container. If Watchtower finds an update for “Nginx” container, it will first shutdown the linked container i.e “wordpress”, and then stop the Nginx container. Then, it will restart both containers in correct order, so that the application comes back up correctly. In our case, the Nginx container will be started first, followed by wordpress container to ensure that the link continued to work. For more details, refer the Watchtower GitHub page link which is given at the end of this guide.

And, that’s all. If you find our guides useful, please share them on your social. professional networks and support OSTechNix. More good stuffs to come. Stay tuned!

Cheers!

Resource:

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You may also like...