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-apprunning 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-appimage and pulls it down. Watchtower then gracefully shuts down the
my-appcontainer and then restarts it using the new image with the port mapping of
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.sockas a volume.
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.sockon the container. By default, Watchtower will check the image registry every 5 minutes. We want it to check every 30 seconds, so use the
-iflag 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.htmlfile. 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
/usr/share/nginx/html/index.htmlon the container. Set
/usr/share/nginx/htmlas the working directory.
FROM nginx COPY index.html /usr/share/nginx/html/index.html WORKDIR /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 login docker 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
docker psto 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.
Now that the
my-web-pagecontainer has been running for a few minutes let’s make a change to the
<html> <header> <title>My We Page</title> </header> <body> This is my web page, how do you like it? </body> </html>
docker psand note how long the container has been running by checking the status. Rebuild the image and push it to Docker Hub:
docker ps docker build -t rivethead42/nginx -f Dockerfile . docker push rivethead42/nginx
Now we wait for Watchtower to detect the changes. Periodically run
docker psto check the container status. When it is less than the original status, we know that Watchtower has worked its magic.
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.