How To Automatically Update Running Docker Containers
Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
Source – ostechnix.com
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.
- How To Install Docker In CentOS
- How To Install Docker In Ubuntu
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 e85c923f2e4dd22a62c12b038ea9694ba9245dcd835f210e1
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.