2323
2424use CliTools \Shell \CommandBuilder \CommandBuilder ;
2525use CliTools \Shell \CommandBuilder \CommandBuilderInterface ;
26+ use CliTools \Utility \DockerUtility ;
2627use CliTools \Utility \PhpUtility ;
2728
2829abstract class AbstractCommand extends \CliTools \Console \Command \AbstractCommand
@@ -35,6 +36,11 @@ abstract class AbstractCommand extends \CliTools\Console\Command\AbstractCommand
3536 */
3637 protected $ dockerPath ;
3738
39+ /**
40+ * @var array
41+ */
42+ protected $ runningContainerCache = array ();
43+
3844 /**
3945 * Search and return for updir docker path
4046 *
@@ -105,7 +111,7 @@ protected function getDockerEnv($containerName, $envName)
105111
106112 if (!empty ($ path )) {
107113 // Genrate full docker container name
108- $ dockerContainerName = \ CliTools \ Utility \DockerUtility:: getDockerInstanceName ($ containerName, 1 , $ path );
114+ $ dockerContainerName = $ this -> findAndBuildContainerName ($ containerName );
109115
110116 // Switch to directory of docker-compose.yml
111117 PhpUtility::chdir ($ path );
@@ -127,6 +133,52 @@ protected function getDockerEnv($containerName, $envName)
127133 return $ ret ;
128134 }
129135
136+ /**
137+ * Check if docker container is available
138+ *
139+ * @param string $containerName Container name
140+ *
141+ * @return string|bool|null
142+ */
143+ protected function checkIfDockerContainerIsAvailable ($ containerName )
144+ {
145+ $ ret = null ;
146+
147+ if (empty ($ containerName )) {
148+ $ this ->output ->writeln ('<p-error>No container specified</p-error> ' );
149+
150+ return false ;
151+ }
152+
153+ try {
154+ // Search updir for docker-compose.yml
155+ $ path = $ this ->getDockerPath ();
156+
157+ if (!empty ($ path )) {
158+ // Genrate full docker container name
159+ $ dockerContainerName = \CliTools \Utility \DockerUtility::getDockerInstanceName ($ containerName , 1 , $ path );
160+
161+ // Switch to directory of docker-compose.yml
162+ PhpUtility::chdir ($ path );
163+
164+ // Get docker confguration (fetched directly from docker)
165+ $ conf = \CliTools \Utility \DockerUtility::getDockerConfiguration ($ dockerContainerName );
166+
167+ if (!empty ($ conf )
168+ && !empty ($ conf ->State )
169+ && !empty ($ conf ->State ->Status )
170+ && in_array (strtolower ($ conf ->State ->Status ), ['running ' ], true )
171+ ) {
172+ $ ret = true ;
173+ }
174+ }
175+ } catch (\Exception $ e ) {
176+ $ ret = false ;
177+ }
178+
179+ return $ ret ;
180+ }
181+
130182 /**
131183 * Execute docker command
132184 *
@@ -154,7 +206,7 @@ protected function executeDockerExec($containerName, CommandBuilderInterface $co
154206
155207 if (!empty ($ path )) {
156208 // Genrate full docker container name
157- $ dockerContainerName = \ CliTools \ Utility \DockerUtility:: getDockerInstanceName ($ containerName, 1 , $ path );
209+ $ dockerContainerName = $ this -> findAndBuildContainerName ($ containerName );
158210
159211 // Switch to directory of docker-compose.yml
160212 PhpUtility::chdir ($ path );
@@ -239,4 +291,37 @@ protected function executeDockerComposeRun($containerName, CommandBuilderInterfa
239291
240292 return 0 ;
241293 }
294+
295+ /**
296+ * @param null $containerName Poissible Container name (csv)
297+ * @return null|string
298+ */
299+ protected function findAndBuildContainerName ($ containerName = null ) {
300+ // Use cached container name
301+ if (isset ($ this ->runningContainerCache [$ containerName ])) {
302+ return $ this ->runningContainerCache [$ containerName ];
303+ }
304+
305+ $ fullContainerName = null ;
306+
307+ $ path = $ this ->getDockerPath ();
308+ $ instancePrefix = \CliTools \Utility \DockerUtility::getDockerInstancePrefix ($ path );
309+
310+ $ containerNameList = PhpUtility::trimExplode (', ' , $ containerName );
311+ foreach ($ containerNameList as $ containerNameToTry ) {
312+ if ($ this ->checkIfDockerContainerIsAvailable ($ containerNameToTry , 'id ' )) {
313+ $ fullContainerName = $ instancePrefix . '_ ' . $ containerNameToTry . '_1 ' ;
314+ break ;
315+ }
316+ }
317+
318+ if (empty ($ fullContainerName )) {
319+ throw new \RuntimeException ('No running docker container found, tried: ' . implode (', ' , $ containerNameList ));
320+ }
321+
322+ // Cache running container
323+ $ this ->runningContainerCache [$ containerName ] = $ fullContainerName ;
324+
325+ return $ fullContainerName ;
326+ }
242327}
0 commit comments