/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { $dockerPath = \CliTools\Utility\DockerUtility::searchDockerDirectoryRecursive(); $lastDockerPath = $this->getApplication()->getSettingsService()->get('docker.up.last'); if (!empty($dockerPath)) { $dockerPath = dirname($dockerPath); } $output->writeln('<h2>Starting docker containers</h2>'); // Stop last docker instance if ($dockerPath && $lastDockerPath) { // Only stop if instance is another one if ($dockerPath !== $lastDockerPath) { $this->stopContainersFromPrevRun($lastDockerPath); } } // Start current docker containers $this->output->writeln('<p>Start docker containers in "' . $dockerPath . '"</p>'); $command = new CommandBuilder(null, 'up -d'); $ret = $this->executeDockerCompose($command); // Store docker path in settings (last docker startup) if ($dockerPath) { $this->getApplication()->getSettingsService()->set('docker.up.last', $dockerPath); } return $ret; }
/** * Execute docker compose run * * @param null|CommandBuilderInterface $command Command * * @return int|null|void */ protected function executeDockerCompose(CommandBuilderInterface $command = null) { // Search updir for docker-compose.yml $path = \CliTools\Utility\DockerUtility::searchDockerDirectoryRecursive(); if (!empty($path)) { $path = dirname($path); $this->output->writeln('<comment>Found docker directory: ' . $path . '</comment>'); // Switch to directory of docker-compose.yml PhpUtility::chdir($path); $command->setCommand('docker-compose'); $command->executeInteractive(); } else { $this->output->writeln('<p-error>No docker-compose.yml found in tree</p-error>'); return 1; } return 0; }
/** * Check if docker exists * * @throws \CliTools\Exception\StopException */ protected function checkIfDockerExists() { $dockerPath = \CliTools\Utility\DockerUtility::searchDockerDirectoryRecursive(); if (!empty($dockerPath)) { $this->output->writeln('<info>Running docker containers:</info>'); // Docker instance found $docker = new CommandBuilder('docker', 'ps'); $docker->executeInteractive(); $answer = ConsoleUtility::questionYesNo('Are these running containers the right ones?', 'no'); if (!$answer) { throw new \CliTools\Exception\StopException(1); } } }
/** * Get docker instance name * * @param string $containerName Container name * @param int $containerNumber Container number * @param NULL|string $path Docker path * * @return string */ public static function getDockerInstanceName($containerName, $containerNumber = 1, $path = null) { $dockerName = array(\CliTools\Utility\DockerUtility::getDockerInstancePrefix($path), (string) $containerName, (int) $containerNumber); return implode('_', $dockerName); }