From ff509e4aa4c7db323ccc04008e315ae39096c3fb Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen.haas@lakedrops.com>
Date: Thu, 12 Jan 2023 16:07:03 +0100
Subject: [PATCH] docker/gitlab-drupal-ci#17 Save solution to find the correct
 self container

---
 src/Handler.php | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/src/Handler.php b/src/Handler.php
index a0ddd44..d473919 100644
--- a/src/Handler.php
+++ b/src/Handler.php
@@ -793,15 +793,26 @@ class Handler extends BaseHandler {
    */
   private function readContainerConfig(): array {
     try {
+      $testString = 'This is a test file for LakeDrops GitLab CI';
+      $filename = '/tmp/' . random_int(100,999) . '.test';
+      file_put_contents($filename, $testString);
       $output = [];
-      exec('basename "$(cat /proc/1/cpuset)"', $output);
-      $id = reset($output);
-      if ($id === '/') {
-        $id = getenv('COMPOSE_PROJECT_NAME') . '_l3d';
+      exec('docker ps -q', $output);
+      $id = NULL;
+      foreach ($output as $id) {
+        $output = [];
+        exec('docker exec ' . $id . ' cat ' . $filename . ' 2>&1', $output);
+        if (reset($output) === $testString) {
+          // Found the container.
+          break;
+        }
+      }
+      unlink($filename);
+      if ($id !== NULL) {
+        $output = [];
+        exec('docker container inspect ' . $id, $output);
+        return json_decode(implode('', $output), TRUE, 512, JSON_THROW_ON_ERROR)[0];
       }
-      $output = [];
-      exec('docker container inspect ' . $id, $output);
-      return json_decode(implode('', $output), TRUE)[0];
     }
     catch (\Exception $ex) {
       // Ignore.
-- 
GitLab