public function testAttachWebsocket() { $containerConfig = new ContainerConfig(); $containerConfig->setImage('busybox:latest'); $containerConfig->setCmd(['sh']); $containerConfig->setAttachStdout(true); $containerConfig->setAttachStderr(true); $containerConfig->setAttachStdin(false); $containerConfig->setOpenStdin(true); $containerConfig->setTty(true); $containerConfig->setLabels(new \ArrayObject(['docker-php-test' => 'true'])); $containerCreateResult = $this->getManager()->create($containerConfig); $webSocketStream = $this->getManager()->attachWebsocket($containerCreateResult->getId(), ['stream' => true, 'stdout' => true, 'stderr' => true, 'stdin' => true]); $this->getManager()->start($containerCreateResult->getId()); // Read the bash first line $webSocketStream->read(); // No output after that so it should be false $this->assertFalse($webSocketStream->read()); // Write something to the container $webSocketStream->write("echo test\n"); // Test for echo present (stdin) $output = ""; while (($data = $webSocketStream->read()) != false) { $output .= $data; } $this->assertContains("echo", $output); // Exit the container $webSocketStream->write("exit\n"); }
/** * Start services for a Job * * @param Job $build */ public function start(Job $build) { foreach ($build->getServices() as $service) { try { $this->docker->getImageManager()->find(sprintf('%s:%s', $service->getRepository(), $service->getTag())); } catch (ClientErrorException $e) { if ($e->getResponse()->getStatusCode() == 404) { $buildStream = $this->docker->getImageManager()->create(null, ['fromImage' => sprintf('%s:%s', $service->getRepository(), $service->getTag())], ImageManager::FETCH_STREAM); $buildStream->onFrame($this->logger->getBuildCallback()); $buildStream->wait(); } else { throw $e; } } $serviceConfig = $service->getConfig(); $containerConfig = new ContainerConfig(); $containerConfig->setImage(sprintf('%s:%s', $service->getRepository(), $service->getTag())); $containerConfig->setLabels(['com.jolici.container=true']); if (isset($serviceConfig['Env'])) { $containerConfig->setEnv($serviceConfig['Env']); } $containerCreateResult = $this->docker->getContainerManager()->create($containerConfig); $this->docker->getContainerManager()->start($containerCreateResult->getId()); $service->setContainer($containerCreateResult->getId()); } }
private function createContainer() { $containerConfig = new ContainerConfig(); $containerConfig->setImage('busybox:latest'); $containerConfig->setCmd(['sh']); $containerConfig->setOpenStdin(true); $containerConfig->setLabels(new \ArrayObject(['docker-php-test' => 'true'])); $containerCreateResult = self::getDocker()->getContainerManager()->create($containerConfig); self::getDocker()->getContainerManager()->start($containerCreateResult->getId()); return $containerCreateResult; }
public function testLogs() { $containerConfig = new ContainerConfig(); $containerConfig->setImage('busybox:latest'); $containerConfig->setCmd(['echo', '-n', 'output']); $containerConfig->setAttachStdout(true); $containerConfig->setLabels(new \ArrayObject(['docker-php-test' => 'true'])); $containerCreateResult = $this->getManager()->create($containerConfig); $this->getManager()->start($containerCreateResult->getId()); $this->getManager()->wait($containerCreateResult->getId()); $logs = $this->getManager()->logs($containerCreateResult->getId(), ['stdout' => true, 'stderr' => true]); $this->assertContains("output", $logs['stdout'][0]); }
/** * Run a build (it's suppose the image exist in docker * * @param Job $job Build to run * @param string|array $command Command to use when run the build (null, by default, will use the command registered to the image) * * @return integer The exit code of the command run inside (0 = success, otherwise it has failed) */ public function run(Job $job, $command) { if (is_string($command)) { $command = ['/bin/bash', '-c', $command]; } $image = $this->docker->getImageManager()->find($job->getName()); $hostConfig = new HostConfig(); $config = new ContainerConfig(); $config->setCmd($command); $config->setImage($image->getId()); $config->setHostConfig($hostConfig); $config->setLabels(new \ArrayObject(['com.jolici.container=true'])); $config->setAttachStderr(true); $config->setAttachStdout(true); $links = []; foreach ($job->getServices() as $service) { if ($service->getContainer()) { $serviceContainer = $this->docker->getContainerManager()->find($service->getContainer()); $links[] = sprintf('%s:%s', $serviceContainer->getName(), $service->getName()); } } $hostConfig->setLinks($links); $containerCreateResult = $this->docker->getContainerManager()->create($config); $attachStream = $this->docker->getContainerManager()->attach($containerCreateResult->getId(), ['stream' => true, 'stdout' => true, 'stderr' => true], ContainerManager::FETCH_STREAM); $attachStream->onStdout($this->logger->getRunStdoutCallback()); $attachStream->onStderr($this->logger->getRunStderrCallback()); $this->docker->getContainerManager()->start($containerCreateResult->getId()); $attachStream->wait(); $containerWait = $this->docker->getContainerManager()->wait($containerCreateResult->getId()); return $containerWait->getStatusCode(); }