public function testCommit() { $container = new Container(); $container->setImage('ubuntu:precise'); $container->setCmd(['/bin/true']); $docker = $this->getDocker(); $manager = $docker->getContainerManager(); $manager->run($container); $manager->wait($container); $image = $docker->commit($container, ['repo' => 'test', 'tag' => 'foo']); $this->assertNotEmpty($image->getId()); $this->assertEquals('test', $image->getRepository()); $this->assertEquals('foo', $image->getTag()); }
/** * Start services for a Job * * @param Job $build */ public function start(Job $build) { foreach ($build->getServices() as $service) { try { $image = $this->docker->getImageManager()->find($service->getRepository(), $service->getTag()); } catch (ImageNotFoundException $e) { $image = $this->docker->getImageManager()->pull($service->getRepository(), $service->getTag(), $this->logger->getBuildCallback()); } $container = new DockerContainer($service->getConfig()); $container->setImage($image); $service->setContainer($container); $this->docker->getContainerManager()->run($container, null, array(), true); } }
/** * Get all containers from the docker daemon * * @param array $params an array of query parameters * * @throws \Docker\Exception\UnexpectedStatusCodeException * * @return Container[] */ public function findAll(array $params = []) { $response = $this->client->get('/containers/json', ['query' => $params]); if ($response->getStatusCode() !== "200") { throw UnexpectedStatusCodeException::fromResponse($response); } $coll = []; $containers = $response->json(); if (!is_array($containers)) { return []; } foreach ($containers as $data) { $container = new Container(); $container->setId($data['Id']); $container->setImage($data['Image']); $container->setCmd((array) $data['Command']); $container->setData($data); $coll[] = $container; } return $coll; }
/** * Run a build (it's suppose the image exist in docker * * @param Job $build 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 $build, $command = null) { $image = $this->docker->getImageManager()->find($build->getRepository(), $build->getTag()); $config = array('HostConfig' => array('Links' => array())); foreach ($build->getServices() as $service) { if ($service->getContainer()) { $config['HostConfig']['Links'][] = sprintf('%s:%s', $service->getContainer()->getRuntimeInformations()['Name'], $service->getName()); } } $container = new DockerContainer($config); if (is_string($command)) { $command = array('/bin/bash', '-c', $command); } if (is_array($command)) { $container->setCmd($command); } $container->setImage($image); $this->docker->getContainerManager()->run($container, $this->logger->getRunCallback(), array(), false, $this->timeout); return $container->getExitCode(); }