#!/usr/bin/env bash # ============================================================= # check-image-updates.sh # Checks all running containers for newer images and caches # results to /tmp/.docker-updates for the MOTD to display. # # Usage: # bash scripts/check-image-updates.sh # # Add to cron for automatic checks (e.g. daily at 3am): # 0 3 * * * bash /opt/stacks/matrix/scripts/check-image-updates.sh # ============================================================= set -euo pipefail CACHE="/tmp/.docker-updates" TMP=$(mktemp) echo "# generated $(date -u +"%Y-%m-%dT%H:%M:%SZ")" > "$TMP" while IFS='|' read -r name image; do OUTPUT=$(docker pull "$image" 2>&1 || true) if echo "$OUTPUT" | grep -q "Downloaded newer image"; then echo "$name → $image" >> "$TMP" fi done < <(docker ps --format '{{.Names}}|{{.Image}}' | sort) mv "$TMP" "$CACHE" UPDATES=$(grep -v '^#' "$CACHE" | grep -c . || true) if [ "$UPDATES" -gt 0 ]; then echo "✅ $UPDATES image(s) updated — cache written to $CACHE" else echo "✅ All images current — cache written to $CACHE" fi