Keep Docker Images Updated with Windows Task Scheduler and Windows Subsystem for Linux

Have you ever wished there was an easy, automated way to keep all your commonly-used Docker images up-to-date? If you’re using Docker Desktop on Windows, you can use the built-in Task Scheduler for this purpose.

Docker container images are updated frequently, depending on the base image you’re using. Each project is updated on a unique schedule, depending on who’s maintaining it. Some of the common Docker images that I use are listed below:

  • ubuntu:focal
  • microsoft/powershell
  • python
  • alpine
  • node
  • codercom/code-server

Software Dependencies

Before we get too far ahead, there are a couple things that you’ll need to setup. First of all, I am using Docker Desktop to set up my Docker environment on Windows, with minimal hassle. I strongly recommend that you download and install Docker Desktop as well. There are two channels that you can select from: Stable and Edge. Because I’m okay with accepting a little extra risk in exchange for the latest features, I’m using the Edge channel.

The other thing you’ll need to install is the Windows Subsystem for Linux (WSL 2). Docker Desktop, at least the Edge channel, integrates with WSL 2 so that you can run Docker commands in Linux. You can still run the Docker executable

After installing Docker Desktop Edge, and WSL 2, make sure you select the WSL 2 integration in the Docker Desktop configuration. Just right-click the Docker Desktop icon in the system tray, choose Settings, and then enable the Use the WSL 2 based engine option.

Create a New Windows Task

Start by creating a new task in the Task Scheduler.

Give the task a useful name and description. I also recommend using the Configure for: Windows 10 option, to ensure that you’re using the latest version of the task configuration.

I tried using the Run whether user is logged on or not option, however using this option caused my task to fail to start. Based on this experience, I asked a question of on the Super User website. Hopefully this bug gets worked out eventually.

One other option I would recommend enabling is the Hidden checkbox. This will prevent console windows from popping up when the task runs. This can be distracting if you’re actively working on your computer when the task kicks off.

Set Up Your Windows Task Trigger

The next thing you’ll need to do is configure the triggers for your task. The triggers determine a schedule or specific set of Windows events that will cause the task to be invoked.

Since we want this to run daily, make sure you choose the On a schedule option. This should be the default. Change the Settings to Daily, and make sure it’s set to recur every 1 days.

Under the Advanced Settings, I recommend adding a randomized delay. I’ve set mine to a 2 hour random delay, so the task isn’t occurring at exactly the same time every day. Another useful option is to stop the task if it runs longer than a certain timespan. Since I’m pulling only a small handful of Docker images, I know that this shouldn’t take more than about 2-3 minutes. As a safety measure, I’ll ask Task Scheduler to kill the task if it runs for more than 30 minutes.

The most important option on this screen is the Enabled checkbox. If this accidentally gets unchecked, then the trigger will never actually work.

Configure the Task Actions

Now that you’ve configured the schedule trigger, head over to the Actions tab. The Actions tab is where you configure the commands that you’d like to run in response to the trigger.

Click the New button, and then type the following command. Of course, you can replace the container image name with one of your choosing.

bash -c "docker pull microsoft/powershell"

When you confirm this window, the Task Scheduler will ask you to move the arguments into the Add arguments field. It’ll do this automatically; you just need to confirm.

When you call bash, you’re actually invoking a command inside the Windows Subsystem for Linux (WSL 2). Using the -c option for Bash will simply run the following command and then terminate the process. This makes it easy to chain multiple commands together.

Another option would be to create a script containing all of the commands that you’d like to run, and then invoke that script using bash. In this case, I’m just keeping things simple and calling the command in-line as an argument.

Call to Action / Next Steps

At this point you’re finished configuring the task! You can add extra actions into the Actions tab of the task inside Windows Task Scheduler. For example, I’ve added several commands to pull some of the common container images that I use.

You can pull additional container images, or perform other automated tasks. For example, you can automatically delete container images that you don’t use frequently, to free up extra disk space. You can also use the Docker CLI to ensure that important containers are running, if you have long-running services on your developer desktop.

The options for automating Docker management are endless!

If you enjoyed this article, head over to my YouTube channel and subscribe to get notified of my latest videos! You can also follow me on Twitter for software tips and tricks.