From 218859a7ea0590f991ed36019a14f750c6510ca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Val=C3=AD=C4=8Dek=20=28YCNet=29?= Date: Tue, 13 Feb 2018 14:42:27 +0100 Subject: [PATCH] Make containers persistent, recreate them only when needed --- launcher-image/src/run-mirror-update | 38 ++++++++++++++++++---------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/launcher-image/src/run-mirror-update b/launcher-image/src/run-mirror-update index 76a42a2..47e1178 100755 --- a/launcher-image/src/run-mirror-update +++ b/launcher-image/src/run-mirror-update @@ -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." +