Add -R flag to skip container recreation after pulling images (#235)

* Added new -R option: Skip Container recreation
* tweaked the logic and messaging

---------

Co-authored-by: mag37 <robin.ivehult@gmail.com>
This commit is contained in:
NapalmZ 2025-11-01 08:28:08 +01:00 committed by GitHub
parent c47e045a3d
commit 39706070cd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -47,6 +47,7 @@ Help() {
echo "-M Prints custom releasenote urls as markdown (requires template support)." echo "-M Prints custom releasenote urls as markdown (requires template support)."
echo "-n No updates; only checking availability without interaction." echo "-n No updates; only checking availability without interaction."
echo "-p Auto-prune dangling images after update." echo "-p Auto-prune dangling images after update."
echo "-R Skip container recreation after pulling images."
echo "-r Allow checking for updates/updating images for docker run containers. Won't update the container." echo "-r Allow checking for updates/updating images for docker run containers. Won't update the container."
echo "-s Include stopped containers in the check. (Logic: docker ps -a)." echo "-s Include stopped containers in the check. (Logic: docker ps -a)."
echo "-t Set a timeout (in seconds) per container for registry checkups, 10 is default." echo "-t Set a timeout (in seconds) per container for registry checkups, 10 is default."
@ -78,6 +79,7 @@ Exclude=${Exclude:-}
DaysOld=${DaysOld:-} DaysOld=${DaysOld:-}
OnlySpecific=${OnlySpecific:-false} OnlySpecific=${OnlySpecific:-false}
SpecificContainer=${SpecificContainer:-""} SpecificContainer=${SpecificContainer:-""}
SkipRecreate=${SkipRecreate:-false}
Excludes=() Excludes=()
GotUpdates=() GotUpdates=()
NoUpdates=() NoUpdates=()
@ -95,7 +97,7 @@ c_blue="\033[0;34m"
c_teal="\033[0;36m" c_teal="\033[0;36m"
c_reset="\033[0m" c_reset="\033[0m"
while getopts "ayfFhiIlmMnprsuvc:e:d:t:x:" options; do while getopts "ayfFhiIlmMnprsuvc:e:d:t:x:R" options; do
case "${options}" in case "${options}" in
a|y) AutoMode=true ;; a|y) AutoMode=true ;;
c) CollectorTextFileDirectory="${OPTARG}" ;; c) CollectorTextFileDirectory="${OPTARG}" ;;
@ -110,6 +112,7 @@ while getopts "ayfFhiIlmMnprsuvc:e:d:t:x:" options; do
M) PrintMarkdownURL=true ;; M) PrintMarkdownURL=true ;;
n) DontUpdate=true; AutoMode=true;; n) DontUpdate=true; AutoMode=true;;
p) AutoPrune=true ;; p) AutoPrune=true ;;
R) SkipRecreate=true ;;
r) DRunUp=true ;; r) DRunUp=true ;;
s) Stopped="-a" ;; s) Stopped="-a" ;;
t) Timeout="${OPTARG}" ;; t) Timeout="${OPTARG}" ;;
@ -554,53 +557,57 @@ if [[ -n "${GotUpdates:-}" ]]; then
docker pull "$ContImage" || { printf "\n%bDocker error, exiting!%b\n" "$c_red" "$c_reset" ; exit 1; } docker pull "$ContImage" || { printf "\n%bDocker error, exiting!%b\n" "$c_red" "$c_reset" ; exit 1; }
done done
printf "\n%bDone pulling updates. %bRecreating updated containers.%b\n" "$c_green" "$c_blue" "$c_reset" printf "\n%bDone pulling updates.%b \n" "$c_green" "$c_reset"
CurrentQue=0 if [[ "$SkipRecreate" == true ]]; then
for i in "${SelectedUpdates[@]}"; do printf "\n%bSkipping container recreation due to -R. \n%bAll done!%b\n" "$c_yellow" "$c_green" "$c_reset"
((CurrentQue+=1)) else
unset CompleteConfs printf "%bRecreating updated containers.%b\n" "$c_blue" "$c_reset"
# Extract labels and metadata CurrentQue=0
ContLabels=$(docker inspect "$i" --format '{{json .Config.Labels}}') for i in "${SelectedUpdates[@]}"; do
ContImage=$(docker inspect "$i" --format='{{.Config.Image}}') ((CurrentQue+=1))
ContPath=$($jqbin -r '."com.docker.compose.project.working_dir"' <<< "$ContLabels") unset CompleteConfs
[[ "$ContPath" == "null" ]] && ContPath="" # Extract labels and metadata
ContConfigFile=$($jqbin -r '."com.docker.compose.project.config_files"' <<< "$ContLabels") ContLabels=$(docker inspect "$i" --format '{{json .Config.Labels}}')
[[ "$ContConfigFile" == "null" ]] && ContConfigFile="" ContImage=$(docker inspect "$i" --format='{{.Config.Image}}')
ContName=$($jqbin -r '."com.docker.compose.service"' <<< "$ContLabels") ContPath=$($jqbin -r '."com.docker.compose.project.working_dir"' <<< "$ContLabels")
[[ "$ContName" == "null" ]] && ContName="" [[ "$ContPath" == "null" ]] && ContPath=""
ContEnv=$($jqbin -r '."com.docker.compose.project.environment_file"' <<< "$ContLabels") ContConfigFile=$($jqbin -r '."com.docker.compose.project.config_files"' <<< "$ContLabels")
[[ "$ContEnv" == "null" ]] && ContEnv="" [[ "$ContConfigFile" == "null" ]] && ContConfigFile=""
ContRestartStack=$($jqbin -r '."mag37.dockcheck.restart-stack"' <<< "$ContLabels") ContName=$($jqbin -r '."com.docker.compose.service"' <<< "$ContLabels")
[[ "$ContRestartStack" == "null" ]] && ContRestartStack="" [[ "$ContName" == "null" ]] && ContName=""
ContOnlySpecific=$($jqbin -r '."mag37.dockcheck.only-specific-container"' <<< "$ContLabels") ContEnv=$($jqbin -r '."com.docker.compose.project.environment_file"' <<< "$ContLabels")
[[ "$ContOnlySpecific" == "null" ]] && ContRestartStack="" [[ "$ContEnv" == "null" ]] && ContEnv=""
ContRestartStack=$($jqbin -r '."mag37.dockcheck.restart-stack"' <<< "$ContLabels")
[[ "$ContRestartStack" == "null" ]] && ContRestartStack=""
ContOnlySpecific=$($jqbin -r '."mag37.dockcheck.only-specific-container"' <<< "$ContLabels")
[[ "$ContOnlySpecific" == "null" ]] && ContRestartStack=""
printf "\n%bNow recreating (%s/%s): %b%s%b\n" "$c_teal" "$CurrentQue" "$NumberofUpdates" "$c_blue" "$i" "$c_reset" printf "\n%bNow recreating (%s/%s): %b%s%b\n" "$c_teal" "$CurrentQue" "$NumberofUpdates" "$c_blue" "$i" "$c_reset"
# Checking if compose-values are empty - hence started with docker run # Checking if compose-values are empty - hence started with docker run
[[ -z "$ContPath" ]] && { echo "Not a compose container, skipping."; continue; } [[ -z "$ContPath" ]] && { echo "Not a compose container, skipping."; continue; }
# cd to the compose-file directory to account for people who use relative volumes # cd to the compose-file directory to account for people who use relative volumes
cd "$ContPath" || { printf "\n%bPath error - skipping%b %s" "$c_red" "$c_reset" "$i"; continue; } cd "$ContPath" || { printf "\n%bPath error - skipping%b %s" "$c_red" "$c_reset" "$i"; continue; }
## Reformatting path + multi compose ## Reformatting path + multi compose
if [[ $ContConfigFile == '/'* ]]; then if [[ $ContConfigFile == '/'* ]]; then
CompleteConfs=$(for conf in ${ContConfigFile//,/ }; do printf -- "-f %s " "$conf"; done) CompleteConfs=$(for conf in ${ContConfigFile//,/ }; do printf -- "-f %s " "$conf"; done)
else else
CompleteConfs=$(for conf in ${ContConfigFile//,/ }; do printf -- "-f %s/%s " "$ContPath" "$conf"; done) CompleteConfs=$(for conf in ${ContConfigFile//,/ }; do printf -- "-f %s/%s " "$ContPath" "$conf"; done)
fi fi
# Check if the container got an environment file set and reformat it # Check if the container got an environment file set and reformat it
ContEnvs="" ContEnvs=""
if [[ -n "$ContEnv" ]]; then ContEnvs=$(for env in ${ContEnv//,/ }; do printf -- "--env-file %s " "$env"; done); fi if [[ -n "$ContEnv" ]]; then ContEnvs=$(for env in ${ContEnv//,/ }; do printf -- "--env-file %s " "$env"; done); fi
# Set variable when compose up should only target the specific container, not the stack # Set variable when compose up should only target the specific container, not the stack
if [[ $OnlySpecific == true ]] || [[ $ContOnlySpecific == true ]]; then SpecificContainer="$ContName"; fi if [[ $OnlySpecific == true ]] || [[ $ContOnlySpecific == true ]]; then SpecificContainer="$ContName"; fi
# Check if the whole stack should be restarted # Check if the whole stack should be restarted
if [[ "$ContRestartStack" == true ]] || [[ "$ForceRestartStacks" == true ]]; then if [[ "$ContRestartStack" == true ]] || [[ "$ForceRestartStacks" == true ]]; then
${DockerBin} ${CompleteConfs} stop; ${DockerBin} ${CompleteConfs} ${ContEnvs} up -d || { printf "\n%bDocker error, exiting!%b\n" "$c_red" "$c_reset" ; exit 1; } ${DockerBin} ${CompleteConfs} stop; ${DockerBin} ${CompleteConfs} ${ContEnvs} up -d || { printf "\n%bDocker error, exiting!%b\n" "$c_red" "$c_reset" ; exit 1; }
else else
${DockerBin} ${CompleteConfs} ${ContEnvs} up -d ${SpecificContainer} || { printf "\n%bDocker error, exiting!%b\n" "$c_red" "$c_reset" ; exit 1; } ${DockerBin} ${CompleteConfs} ${ContEnvs} up -d ${SpecificContainer} || { printf "\n%bDocker error, exiting!%b\n" "$c_red" "$c_reset" ; exit 1; }
fi fi
done done
if [[ "$AutoPrune" == false ]] && [[ "$AutoMode" == false ]]; then printf "\n"; read -rep "Would you like to prune all dangling images? y/[n]: " AutoPrune; fi if [[ "$AutoPrune" == false ]] && [[ "$AutoMode" == false ]]; then printf "\n"; read -rep "Would you like to prune all dangling images? y/[n]: " AutoPrune; fi
if [[ "$AutoPrune" == true ]] || [[ "$AutoPrune" =~ [yY] ]]; then printf "\nAuto pruning.."; docker image prune -f; fi if [[ "$AutoPrune" == true ]] || [[ "$AutoPrune" =~ [yY] ]]; then printf "\nAuto pruning.."; docker image prune -f; fi
printf "\n%bAll done!%b\n" "$c_green" "$c_reset" printf "\n%bAll done!%b\n" "$c_green" "$c_reset"