@@ -184,10 +184,11 @@ protected function checkIfDockerContainerIsAvailable($containerName)
184184 *
185185 * @param string $containerName Container name
186186 * @param CommandBuilderInterface $comamnd Command
187+ * @param callback|null $dockerCommandCallback Docker command callback
187188 *
188189 * @return int|null|void
189190 */
190- protected function executeDockerExec ($ containerName , CommandBuilderInterface $ command )
191+ protected function executeDockerExec ($ containerName , CommandBuilderInterface $ command, callable $ dockerCommandCallback = null )
191192 {
192193 if (empty ($ containerName )) {
193194 $ this ->output ->writeln ('<p-error>No container specified</p-error> ' );
@@ -216,7 +217,12 @@ protected function executeDockerExec($containerName, CommandBuilderInterface $co
216217 ) . '" in docker container " ' . $ dockerContainerName . '" ...</info> '
217218 );
218219
219- $ dockerCommand = new CommandBuilder ('docker ' , 'exec -ti %s ' , array ($ dockerContainerName ));
220+ $ dockerCommand = new CommandBuilder ('docker ' , 'exec -ti ' );
221+ if ($ dockerCommandCallback ) {
222+ $ dockerCommandCallback ($ dockerCommand );
223+ }
224+ $ dockerCommand ->addArgument ($ dockerContainerName );
225+
220226 $ dockerCommand ->append ($ command , false );
221227 $ dockerCommand ->executeInteractive ();
222228 } else {
@@ -296,7 +302,8 @@ protected function executeDockerComposeRun($containerName, CommandBuilderInterfa
296302 * @param null $containerName Poissible Container name (csv)
297303 * @return null|string
298304 */
299- protected function findAndBuildContainerName ($ containerName = null ) {
305+ protected function findAndBuildContainerName ($ containerName = null )
306+ {
300307 // Use cached container name
301308 if (isset ($ this ->runningContainerCache [$ containerName ])) {
302309 return $ this ->runningContainerCache [$ containerName ];
@@ -305,16 +312,27 @@ protected function findAndBuildContainerName($containerName = null) {
305312 $ fullContainerName = null ;
306313
307314 $ path = $ this ->getDockerPath ();
308- $ instancePrefix = \CliTools \Utility \DockerUtility::getDockerInstancePrefix ($ path );
315+ $ oldPath = getcwd ();
316+
317+ chdir ($ path );
309318
310319 $ containerNameList = PhpUtility::trimExplode (', ' , $ containerName );
311320 foreach ($ containerNameList as $ containerNameToTry ) {
312- if ($ this ->checkIfDockerContainerIsAvailable ($ containerNameToTry , 'id ' )) {
313- $ fullContainerName = $ instancePrefix . '_ ' . $ containerNameToTry . '_1 ' ;
321+ try {
322+ $ command = new CommandBuilder ('docker-compose ' );
323+ $ command
324+ ->addArgumentTemplate ('ps -q %s ' , $ containerNameToTry )
325+ ->setOutputRedirect (CommandBuilder::OUTPUT_REDIRECT_NO_STDERR );
326+ $ fullContainerName = $ command ->execute ()->getOutputString ();
314327 break ;
328+ } catch (\Exception $ e ) {
329+ // container not running
330+ continue ;
315331 }
316332 }
317333
334+ chdir ($ oldPath );
335+
318336 if (empty ($ fullContainerName )) {
319337 throw new \RuntimeException ('No running docker container found, tried: ' . implode (', ' , $ containerNameList ));
320338 }
0 commit comments