|
1 | 1 | # - This script is used to update the Home Assistant Docker containers |
2 | 2 | # - It will pull the latest images, check if the image ID has changed, and restart the container if needed |
3 | | -# - It will also cleanup unused resources after the update |
4 | 3 |
|
5 | 4 | # Original Repo: https://github.com/CCOSTAN/Home-AssistantConfig |
6 | 5 | # Follow me on https://www.vcloudinfo.com/click-here |
7 | 6 |
|
8 | 7 | #!/bin/bash |
9 | 8 |
|
| 9 | +set -euo pipefail |
| 10 | + |
10 | 11 | # Update system packages |
11 | 12 | sudo apt-get update && sudo apt-get upgrade -y |
12 | 13 |
|
| 14 | +DC=(docker compose) |
| 15 | + |
13 | 16 | # Pull the latest images |
14 | | -docker-compose pull |
| 17 | +"${DC[@]}" pull |
15 | 18 |
|
16 | 19 | # Get list of services from docker-compose.yml |
17 | | -EXISTING_SERVICES=$(docker-compose config --services) |
| 20 | +EXISTING_SERVICES=$("${DC[@]}" config --services) |
18 | 21 |
|
19 | 22 | # Get list of running service containers |
20 | | -RUNNING_CONTAINERS=$(docker-compose ps --services) |
| 23 | +RUNNING_CONTAINERS=$("${DC[@]}" ps --services) |
21 | 24 |
|
22 | 25 | # Loop through each running service and check if its image has changed |
23 | 26 | for service in $RUNNING_CONTAINERS; do |
24 | 27 | if echo "$EXISTING_SERVICES" | grep -qw "$service"; then |
25 | 28 | # Get the current running image ID (remove sha256: prefix) |
26 | 29 | CURRENT_IMAGE_ID=$(docker inspect --format='{{.Image}}' "$service" 2>/dev/null | sed 's/^sha256://') |
27 | 30 |
|
28 | | - # Get the latest image ID from docker-compose |
29 | | - LATEST_IMAGE_ID=$(docker-compose images -q "$service" 2>/dev/null) |
| 31 | + # Get the latest image ID from docker compose |
| 32 | + LATEST_IMAGE_ID=$("${DC[@]}" images -q "$service" 2>/dev/null) |
30 | 33 |
|
31 | 34 | # If the image ID is different, restart the container |
32 | 35 | if [ "$CURRENT_IMAGE_ID" != "$LATEST_IMAGE_ID" ] && [ -n "$LATEST_IMAGE_ID" ]; then |
33 | 36 | echo "Updating container: $service" |
34 | | - docker-compose stop "$service" |
35 | | - docker-compose rm -f "$service" |
36 | | - docker-compose up -d "$service" |
| 37 | + "${DC[@]}" stop "$service" |
| 38 | + "${DC[@]}" rm -f "$service" |
| 39 | + "${DC[@]}" up -d "$service" |
37 | 40 | else |
38 | 41 | echo "No update needed for: $service" |
39 | 42 | fi |
40 | 43 | else |
41 | 44 | echo "Skipping non-existent service: $service" |
42 | 45 | fi |
43 | 46 | done |
44 | | - |
45 | | -# Cleanup unused resources |
46 | | -docker container prune -f |
47 | | -docker image prune -f |
48 | | -docker volume prune -f |
0 commit comments