Skip to content

Instantly share code, notes, and snippets.

@ChrisSwanson
Created September 14, 2024 01:41
Show Gist options
  • Save ChrisSwanson/11be547b8ddffe872608997eaa370af1 to your computer and use it in GitHub Desktop.
Save ChrisSwanson/11be547b8ddffe872608997eaa370af1 to your computer and use it in GitHub Desktop.
Lazy Cron Update docker containers
#!/bin/bash
# Navigate to the directory containing the docker-compose.yaml file
cd /path/to/docker-compose.yaml || exit
# Pull the latest images for all services
docker-compose pull
# Get the list of services from the docker-compose file
services=$(docker-compose config --services)
# Iterate through each service
for service in $services; do
# Get the current image of the running container: awk for sha256 beginning of string.
current_image=$(docker inspect --format='{{.Image}}' "$service" 2>/dev/null | awk -F':' '{print $2}')
# Get the latest image for the service
latest_image=$(docker-compose images -q "$service")
# Check if the current image is different from the latest image
if [[ "$current_image" != "$latest_image" ]]; then
echo "Updating $service..."
# Recreate the container with the new image
docker-compose up -d --no-deps --force-recreate "$service"
else
echo "$service is up-to-date."
fi
done
echo "All services checked."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment