#!/usr/bin/env bash set -e # Our function to update containers based on their base image update_container() { local image=$1 docker pull $image local updated_containers=0 # Loop through all running containers for container in $(docker ps --format "{{.Names}}"); do local container_image=$(docker inspect --format '{{.Config.Image}}' "$container") # We check if the current container's image matches the updated image if [[ "$container_image" == "$image" ]]; then local latest=$(docker inspect --format "{{.Id}}" $image) local running=$(docker inspect --format "{{.Image}}" $container) if [[ "$running" != "$latest" ]]; then echo "Upgrading $container" #docker rm -f $container # docker run --name $container $image ((updated_containers++)) fi fi done if [[ $updated_containers -eq 0 ]]; then echo "No containers updated for $image" else echo "$updated_containers container(s) updated for $image" fi } # Our main script starts here # Check for updates to all images used by running containers for image in $(docker ps --format '{{.Image}}' | sort | uniq); do echo "Checking updates for $image" update_container $image done echo "Container update check complete."