public function getDocker() { $client = Client::createWithEnv(); if (null === $this->docker) { $this->docker = new Docker($client); } return $this->docker; }
public function getDocker() { if (!$this->docker) { $this->docker = new Docker(DockerClient::createWithEnv()); } return $this->docker; }
/** * Run a docker image * @param string[] $cmd The list of commands to invoke in the docker image */ private function runAudfprint($cmd) { // Set up the log storage $logs = []; // Are we using audfprint directly? if (env('AUDFPRINT_PATH') != "") { // Run audfprint directly $command = env('AUDFPRINT_PATH') . " " . implode(" ", $cmd); exec($command, $output, $status_code); if ($status_code != 0) { throw new \Exception("Attempted to run audfprint directly (" . $command . "), exited with status code: " . $status_code . " - " . json_encode($output)); } // Take the results and use them $logs = array_merge($logs, $output); } else { // We are using docker instead // Set up a guzzle connection with proper configuration // TODO: move timeouts to env setting $docker_client = \Docker\Http\DockerClient::createWithEnv(); $docker_client->setDefaultOption('timeout', 3600); $docker_client->setDefaultOption('connect_timeout', 3600); // Create a connection with docker $docker = new \Docker\Docker($docker_client); // Create the docker container $container = new \Docker\Container(['Image' => env('DOCKER_FPRINT_IMAGE'), 'Cmd' => $cmd, 'Volumes' => ['/var/audfprint' => []], 'HostConfig' => ['Binds' => [env('FPRINT_STORE') . ':' . AudfprintFingerprinter::AUDFPRINT_DOCKER_PATH]]]); $manager = $docker->getContainerManager(); // Gather the logs and return them to the caller $manager->run($container, function ($output, $type) use(&$logs) { // TODO: Process output more intelligently... $logs = array_merge($logs, explode("\n", $output)); }); // Clean up after yourself, it's only polite try { $manager->stop($container); $manager->remove($container, false, true); } catch (\Exception $e) { // Apparently sometimes containers don't remove, but we don't want to error when that happens. // TODO: figure out why container removal fails on occasion } } return $logs; }