From a22188c026033d3d016055b46feff93607337977 Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Thu, 3 Jan 2019 15:27:49 +0100
Subject: [PATCH] #9 Remove all outdated containers when an image got updated

---
 l3d | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/l3d b/l3d
index 365b1ba..7aa4d26 100755
--- a/l3d
+++ b/l3d
@@ -48,6 +48,7 @@ fi
 ID=$(docker container ls -q -f name=${COMPOSE_PROJECT_NAME}_l3d)
 
 if [[ "$1" == "update" ]]; then
+  IMAGEID=$(docker image ls -q registry.lakedrops.com/docker/l3d:php-${PHP_VERSION})
   echo "Updating the image ..."
   STATUS=$(docker pull registry.lakedrops.com/docker/l3d:php-${PHP_VERSION})
   if [[ "$STATUS" == *"Status: Image is up to date"* ]]; then
@@ -55,10 +56,16 @@ if [[ "$1" == "update" ]]; then
   else
     echo "Image updated"
     if [[ -n "$ID" ]]; then
-      echo "Removing outdated container ..."
-      docker stop ${COMPOSE_PROJECT_NAME}_l3d
-      docker rm ${COMPOSE_PROJECT_NAME}_l3d
-      ID=
+      while true; do
+        ID=$(docker container ls -q -f ancestor=${IMAGEID})
+        if [[ -n "$ID" ]]; then
+          echo "Removing outdated container ..."
+          docker stop ${ID}
+          docker rm ${ID}
+        else
+          break
+        fi
+      done
     fi
   fi
 fi
-- 
GitLab