Skip to main content

Using Watchtower to Keep Containers up to Date

Hands-On Lab

 

Photo of Travis Thomsen

Travis Thomsen

Course Development Director in Content

Length

01:00:00

Difficulty

Intermediate

Tired of having to manually update several containers, you’ve decided to look for an automated solution. After doing some research, you have discovered Watchtower. Watchtower is a container that updates all running containers when changes are made to the image that it is running. You will need to create a Dockerfile that will be used to create a Docker image. The image will be pushed to Docker Hub. Next, you will create a container using this image. Once the image is created, you will deploy the Watchtower container. After Watchtower is deployed, you will update the Dockerfile, rebuild the image, and push the changes to Docker Hub. Watchtower checks for changes every 30 seconds. Once it detects the changes, Watchtower will update the running container. For more information on Watchtower you can go here.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Using Watchtower to Keep Containers up to Date

Introduction

We're tired of having to manually update several containers, so we've decided to look for an automated solution.

After doing some research, we have discovered Watchtower, a container that updates all running containers when changes are made to the image that it is running.

We need to create a Dockerfile that will be used to create a Docker image. The image will be pushed to Docker Hub.

Next, you will create a container using this image. Once the image is created, you will deploy the Watchtower container. After Watchtower is deployed, you will update the Dockerfile, rebuild the image, and push the changes to Docker Hub.

Watchtower checks for changes every 30 seconds. Once it detects the changes, Watchtower will update the running container.

For more information on Watchtower you can go here.

Logging Into the Lab

First make sure that you have a Docker Hub account. We'll need it to complete the lab. Then log into the environment using the credentials provided on the lab overview page, either in a terminal session on your local machine or by clicking Instant Terminal.

In this lab, we will be using Watchtower to monitor containers for updates. In order to complete this learning activity, we will need a Docker Hub account.

In the home directory of cloud_user we will find the lab directory. Change to that directory.

Create the Dockerfile

We need to create a Dockerfile (with any text editor you like), and put the following into it:

FROM node
RUN mkdir -p /var/node
ADD src/ /var/node/
WORKDIR /var/node
RUN npm install
EXPOSE 3000
CMD ./bin/www

Log in to Docker Hub

[cloud_user@host]$ docker login -u [DOCKER_HUB_USERNAME]

Build the Docker Image

[cloud_user@host]$ docker image build -t [USERNAME]/lab-watchtower -f Dockerfile .

Push the Image to Docker Hub

docker image push USERNAME/lab-watchtower

Create a Demo Container

Create the container that Watchtower will monitor:

[cloud_user@host]$ docker container run -d --name demo-app -p 80:3000 --restart always USERNAME/lab-watchtower

Make sure it's running:

[cloud_user@host]$ docker container ls

Create the Watchtower Container

This next container will be the one monitoring the demo-app container. Start it up with this:

[cloud_user@host]$ docker run -d --name watchtower --restart always -v /var/run/docker.sock:/var/run/docker.sock v2tec/watchtower -i 30

Update the Docker Image

We need to edit Dockerfile, making a change that Watchtower will see and implement. Add a mkdir command after the one that's already in there:

FROM node
RUN mkdir -p /var/node
RUN mkdir -p /var/test
ADD src/ /var/node/
WORKDIR /var/node
RUN npm install
EXPOSE 3000
CMD ./bin/www

Rebuild the image:

[cloud_user@host]$ docker build -t [USERNAME]/lab-watchtower -f Dockerfile .

Push the new image:

[cloud_user@host]$ docker image push [USERNAME]/lab-watchtower

Checking

Now if we run docker container ls periodically, we'll eventually see that the demo-app container has a newer CREATED time than the watchtower container does. This means that Watchtower saw the change we made, and restarted the other container using the new image.

Conclusion

Our days of manually updating containers are over! Watchtower will see any changes we make to images, then go ahead and implement them automatically. Congratulations!