Make containers persistent, recreate them only when needed

This commit is contained in:
Václav Valíček (YCNet) 2018-02-13 14:42:27 +01:00
parent 13275017f6
commit 218859a7ea
No known key found for this signature in database
GPG Key ID: 7CF44871CEA75938

View File

@ -26,6 +26,9 @@ trap finish EXIT
volume_prefix=cloner
clone_prefix=cloner-runner
# last version of image
last_version=$(docker images $mirror_image --no-trunc --quiet)
name=${1:-}
[ -n "$name" ] || die "No volume specified"
@ -47,21 +50,28 @@ then
fi
unset lines
# does it exist?
docker ps --quiet -a --no-trunc --filter name=^/$container_name$ > $scratch/existing
lines=$(wc -l $scratch/existing | cut -d' ' -f1)
if ! [ $lines -eq 0 ]
then
log "Conflicting container exists, removing.."
docker rm $(cat $scratch/existing)
fi
unset lines
# does volume exist?
# check if volume does exist?
docker volume inspect $name > /dev/null 2>&1 || die "Volume '$name' does not exist - exiting!"
# does it exist?
image_version=$(docker inspect --format='{{.Image}}' $container_name 2>/dev/null || true)
# do I need to recreate image?
if ! [ "x$image_version" = "x$last_version" ]
then
# not empty string - need to delete first
if [ -n "$image_version" ]
then
log "Conflicting container exists, removing.."
docker rm $container_name
fi
# create new container
log "Creating new version of container.."
docker create -v $name:/data --name $container_name $mirror_image
fi
log "Running container of update process..."
docker run -v $name:/data --name $container_name --rm $mirror_image | while read line; do log "$line"; done
docker start -a $container_name | while read line; do log "$line"; done
log "Finished."