Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Using Watchtower To Keep Your Containers Up To Date

Jun 08, 2023 • 4 Minute Read

Please set an alt value for this image...
  • Software Development
  • Cloud

Watchtower is an application that monitors your running containers for changes to the image. When you push an update to your Docker registry, Watchtower detects the changes and pulls down the new copy of the image. It will then restart the container using the new image and the same start options of the original.
For example, let's say you have a containerized application called my-app running with a port mapping of 8090:3000. Your team uses CI/CD to roll out daily updates of the application. After your iteration is complete, you tag and push the image up to the registry. Watchtower has been configured to check the registry every 60 seconds. Once the 60-second iteration is complete, it detects the changes to the my-app image and pulls it down. Watchtower then gracefully shuts down the my-app container and then restarts it using the new image with the port mapping of 8090:3000.
For all of this to work, Watchtower needs to a have access to the Docker API. When creating the Watchtower container, you will need to mount /var/run/docker.sock as a volume.
 

Using Watchtower

Now, let's go and get Watchtower running. You can use your local system if you have Docker already installed or one of your cloud servers using the Docker Certification Distribution as the image. In the example below, we will create a Watchtower container that will use the following startup options. The container will always be restarted. It will mount  /var/run/docker.sock to /var/run/docker.sock on the container. By default, Watchtower will check the image registry every 5 minutes. We want it to check every 30 seconds, so use the -i flag  with the poll interval set to 30. For a full listing of Watchtower’s options, please visit the GitHub readme.
docker run -d --name watchtower   --restart always   -v /var/run/docker.sock:/var/run/docker.sock   v2tec/watchtower   -i 30
Now that the Watchtower container is running let's go and create a container for it to monitor. On your system, create an index.html file. For example:
<html>  <header>    <title>My We Page</title>  </header>  <body>    This is my web page.  </body></html>
Now create a Dockerfile that uses nginx as the base image. Copy over the index.html file to /usr/share/nginx/html/index.html on the container. Set /usr/share/nginx/html as the working directory.

Dockerfile:

FROM nginxCOPY index.html /usr/share/nginx/html/index.htmlWORKDIR /usr/share/nginx/html
Once the Dockerfile is ready, build your image. You will want to tag the image with either your Docker Hub username or the full URL to the image on your private registry. In the example below, I’ll be using my Docker Hub username.
docker build -t rivethead42/nginx -f Dockerfile .
Log in and push the image:
docker logindocker push rivethead42/nginx
Create a container using the image you pushed to the registry:
docker run -d —name my-web-page -p 80:80 —restart always rivethead42/nginx
Execute docker ps to make sure your container is running. After verifying that the container is running, you can wait a few minutes before making any changes. We'll be checking the status of the container to verify that Watchtower has updated it.
docker ps
Now that the my-web-page container has been running for a few minutes let's make a change to the index.html file.
<html>  <header>    <title>My We Page</title>  </header>  <body>    This is my web page, how do you like it?  </body></html>
Execute docker ps and note how long the container has been running by checking the status. Rebuild the image and push it to Docker Hub:
docker psdocker build -t rivethead42/nginx -f Dockerfile .docker push rivethead42/nginx
Now we wait for Watchtower to detect the changes. Periodically run docker ps to check the container status. When it is less than the original status, we know that Watchtower has worked its magic.
docker ps
As you can see, it’s pretty easy to keep containers up to date by using Watchtower. If you want to learn about other things you can do with Docker, check out my latest course Learn Docker By Doing.
TravisThomsen

TravisThomsen

More about this author