How to update a lot of docker-compose services like a breeze

I use docker-compose to manage services on a majority of my servers. But I find it hard to keep the containers up-to-date. So I wrote this simple bash script, that rebuilds and thus updates all the docker-compose projects in a directory. It even has an option to ignore some of the folders by putting a file named .docker-compose-updater-ignore in them. I decided to publish it here as someone might find this useful.

#!/bin/bash

# docker-compose-updater - sijisu.eu - 2020
# for every folder in pwd that has docker-compose.yml, it pulls new images and recreates the container
# you can add .docker-compose-updater-ignore file to a folder that you don't want to get updated
# it cleans up afterwards

origin_path=$(pwd)

if [ -z "$1" ]; then
        update_path=$(pwd);
else
        update_path=$1
fi

echo "=== starting docker-compose-updater";
echo "=== working path: $update_path"

for dir_container in $(find $update_path -maxdepth 1 -mindepth 1 -type d -exec test -e "{}/docker-compose.yml" ';' '!' -exec test -e "{}/.docker-compose-updater-ignore" ';' -print);
        do
                echo "=== [>] working on $dir_container";
                cd $dir_container;
                echo "    [+] pulling latest images"
                docker-compose pull
                echo "    [+] recreating"
                docker-compose up --force-recreate --build -d;
                echo "=== [<] working on $dir_container";
done

echo "=== cleaning up..."
docker image prune -f
cd $origin_path
echo "=== done!"

Published on
linux

Latest Posts