From ce70ebd6a187dc2b94934b1a76ac11391cec103a Mon Sep 17 00:00:00 2001 From: Daniel McCormack <52194107+demccormack@users.noreply.github.com> Date: Sun, 14 May 2023 13:57:51 +1200 Subject: [PATCH] Move container cleanup step There should be no interim commands between taking down the system and running it again because they increase downtime and may even fail, blocking deployment while the system is down. This moves all cleanup commands to the end. It might fail and then it will block deployment. Also it shouldn't run at this point because it increases downtime even if it doesn't fail. --- build_and_run.sh | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/build_and_run.sh b/build_and_run.sh index b6e4556..3acbb0b 100644 --- a/build_and_run.sh +++ b/build_and_run.sh @@ -37,19 +37,21 @@ build $FRONTEND_IMAGE ! [[ "$(docker ps | grep piplayer)" ]] || docker-compose --env-file $ENV down -# Clear out old containers -CONTAINERS=$(docker ps -aq) || true -! [[ "$CONTAINERS" ]] || docker rm $CONTAINERS - docker-compose --env-file="$ENV" ${DOCKER_COMPOSE_FILES:-} up --force-recreate -d -docker ps - -# Clear out old images +# Clear out old containers and images +CONTAINERS=$(docker ps -a | grep -v 'piplayer.*latest' | tail +2 | awk '{print $1}') || true +if [[ "$CONTAINERS" ]] +then + docker stop $CONTAINERS + docker rm $CONTAINERS +fi IMAGES=$(docker images | grep '' | awk '{print $3}') || true ! [[ "$IMAGES" ]] || docker rmi $IMAGES +# Report results [[ "${PROXY_WAS:-}" == "$(image_of proxy)" ]] && echo "Proxy unchanged" || echo "Proxy has new image" [[ "${BACKEND_WAS:-}" == "$(image_of backend)" ]] && echo "Backend unchanged" || echo "Backend has new image" [[ "${FRONTEND_WAS:-}" == "$(image_of $FRONTEND_IMAGE)" ]] && echo "Frontend unchanged" || echo "Frontend has new image" +docker ps echo "Done"