Auto-Updating Docker Containers

Docker is the biggest advance to production software engineering in the past decade. If you're like me though, you slept on it this entire time and are now feeling like you're too far behind to catch up and finally start using them. I'm going to make a few short posts on the rudimentary basics to get up and running with Docker, creating a Dockerfile, publishing it to Docker Hub and deploying/running it live on a server. This post will show you how to set up a docker-compose.yaml file which will automatically update when a new image is pushed to Docker Hub. Let's go!

Photo by Andy Mai / Unsplash

First of all, I would not suggest doing this for anything you want to remain stable or have any decent uptime on. This is to keep up to date with the latest bleeding edge of a project and can be useful for development and side projects when you merge your code you can show it to people live a few minutes later. If you do this with containers you're looking to rely on (such as Matrix, Vikunja or Home Assistant (ohh, foreshadowing?)), you can easily see how you could accidentally deploy to a breaking version and have to do some messy sysadmin work! However, there are valid reasons to automatically update to the latest tag, and to do this, we will be using Watchtower which is a Docker container that monitors other containers and if their image updates, then will SIGTERM it, pull down the newest version and restart it. Pretty meta! Onto the code:

version: '3.3'

services:
  via-web:
    container_name: "via-web"
    image: conorjf/via-web:latest
    restart: unless-stopped

    volumes:
      - /var/log/instance_logs/via:/var/log

  via-watchtower:
    container_name: "via-watchtower"
    image: containrrr/watchtower
    restart: unless-stopped

    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

    command: via-web --interval 30

This relatively simple docker-compose.yaml shows how easy this is. We define two services, Via and Watchtower. Via's image is set to latest from Docker Hub and it uses a simple trick to easily view logs explained here. This is clearly something under development and so it's safe to set it up to deploy latest blindly. The via-watchtower service is also very simple, really only requiring 2 statements apart from the image. The volumes is so Watchtower can be informed about Docker updates/changes in state, etc and the command is of the format <container to watch for updates on> --interval <seconds to check for updates>. It's that easy! Using this and some continuous integration to build and deploy the Docker image, Via is kept constantly up to date for development and beta testing.

I hope this was instructive and please reach out if you have any questions, suggestions or comments in general :)

Show Comments