From b603381c5e1170dd4bfaecba8b9b8b8bcf4e4153 Mon Sep 17 00:00:00 2001 From: danny28506 Date: Wed, 29 Apr 2026 11:11:24 -0400 Subject: [PATCH] init --- update-docker-containers.sh | 49 +++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 update-docker-containers.sh diff --git a/update-docker-containers.sh b/update-docker-containers.sh new file mode 100755 index 0000000..04c2e5d --- /dev/null +++ b/update-docker-containers.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +# --- CONFIGURATION --- +# Add the paths to the directories containing your docker-compose.yml files +# Example: SERVICES=("/home/user/homeassistant" "/home/user/nginx") +SERVICES=( + "/root/" +) + +# Color codes for better readability +GREEN='\033[0;32m' +RED='\033[0;31m' +NC='\033[0m' # No Color + +echo "Starting Docker services update..." + +for dir in "${SERVICES[@]}"; do + if [ -d "$dir" ] && [ -f "$dir/docker-compose.yml" ]; then + echo -e "\nUpdating service in: ${GREEN}$dir${NC}" + + # Move into the directory + cd "$dir" || continue + + # 1. Pull the latest images + if docker compose pull; then + echo "Images pulled successfully." + + # 2. Recreate containers with the new images + # --detach: runs in background + # --remove-orphans: cleans up old services no longer in the file + if docker compose up -d --remove-orphans; then + echo -e "${GREEN}Successfully updated!${NC}" + else + echo -e "${RED}Failed to restart containers.${NC}" + fi + else + echo -e "${RED}Failed to pull images. Skipping this service.${NC}" + fi + else + echo -e "\n${RED}Error:${NC} Directory or docker-compose.yml not found at $dir" + fi +done + +# 3. Cleanup +echo -e "\nCleaning up unused images..." +docker image prune -f + +echo -e "\n${GREEN}All updates complete!${NC}" +