Update all your Docker Compose containers with a single script

Update all your Docker Compose containers with a single script
Photo by Rinson Chory / Unsplash

I run my homelab on Docker Compose where all my files are located in a /hom/data/docker-compose/**/docker-compose.yaml structure.

To update these containers I often just, every two weeks, go into each folder, run docker compose pull && docker compose up -d and then run docker image prune to remove dangling images afterwards.

There are alternatives like watchtower which auto update these outdated images, but for something as critical as my homelab components I like to be in control manually.

Partly that is.

Here is a quick bash script I boggled together that does that for each compose.yaml config found in a given folder:

#!/usr/bin/env bash
set -e

function print_line_break() {
  # shellcheck disable=SC2183
  printf '%20s\n' | tr ' ' -
}

readarray -d '' composeConfigs < <(find . -maxdepth 2 -type f -name "compose.yaml" -print0)

for cfg in "${composeConfigs[@]}"; do
  echo ""
  echo "[Updating stack for: $cfg]"
  print_line_break
  docker compose -f "$cfg" pull
  docker compose -f "$cfg" up -d
done

echo "Pruning old images..."
docker image prune -af

Run it with /home/data/docker-compose/update_containers.sh or put it in a cron job 🐳.

Full demo on my Github demo collection.

This script can optionally take a parameter to make the folder adjustable instead of running in current dir. I keep it in the same folder so didn't bother.