/** * 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); } }
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()); }
public function __construct(Build $build, $cmd = null) { $config = ['Memory' => 256 * 1024 * 1204, 'Image' => $build->getImageName(), 'Env' => ['SYMFONY_ENV=prod']]; if (null !== $cmd) { $config['Cmd'] = $cmd; } parent::__construct($config); }
/** * Rename a container (API v1.17) * * @param Container $container * @param string $newname * * @throws \Docker\Exception\UnexpectedStatusCodeException */ public function rename(Container $container, $newname) { $response = $this->client->post(['/containers/{id}/rename?name={newname}', ['id' => $container->getId(), 'newname' => $newname]]); if ($response->getStatusCode() !== "204") { throw UnexpectedStatusCodeException::fromResponse($response); } return $this; }
/** * Unpause a container * * @param Container $container * * @throws \Docker\Exception\UnexpectedStatusCodeException */ public function unpause(Container $container) { $response = $this->client->post(['/containers/{id}/unpause', ['id' => $container->getId()]]); if ($response->getStatusCode() !== "204") { throw UnexpectedStatusCodeException::fromResponse($response); } $this->inspect($container); return $this; }
/** * Commit a container into an image * * @param \Docker\Container $container * @param array $config * * @throws Exception\UnexpectedStatusCodeException * * @return \Docker\Image * * @see http://docs.docker.com/reference/api/docker_remote_api_v1.7/#create-a-new-image-from-a-containers-changes */ public function commit(Container $container, $config = []) { if (isset($config['run'])) { $config['run'] = json_encode($config['run']); } $config['container'] = $container->getId(); $response = $this->httpClient->post(['/commit{?config*}', ['config' => $config]]); if ($response->getStatusCode() !== "201") { throw new UnexpectedStatusCodeException($response->getStatusCode(), (string) $response->getBody()); } $image = new Image(); $image->setId($response->json()['Id']); if (array_key_exists('repo', $config)) { $image->setRepository($config['repo']); } if (array_key_exists('tag', $config)) { $image->setTag($config['tag']); } return $image; }
/** * @param Container $container * * @return array */ private function getContainerHosts(Container $container) { $inspection = $container->getRuntimeInformations(); $hosts = [substr($container->getName(), 1) . $this->tld]; if (isset($inspection['Config']['Env']) && is_array($inspection['Config']['Env'])) { $env = $inspection['Config']['Env']; foreach (preg_grep('/DOMAIN_NAME=/', $env) as $row) { $row = substr($row, strlen('DOMAIN_NAME=')); $hosts = array_merge($hosts, explode(',', $row)); } } return $hosts; }
/** * Send a signal to container * * @param Container $container * @param string $signal * * @throws \Docker\Exception\UnexpectedStatusCodeException */ public function kill(Container $container, $signal = "SIGKILL") { $response = $this->client->post(['/containers/{id}/kill?signal={signal}', ['id' => $container->getId(), 'signal' => $signal]]); if ($response->getStatusCode() !== "204") { throw UnexpectedStatusCodeException::fromResponse($response); } }
/** * 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(); }
public function __construct(Build $build) { parent::__construct(['Memory' => 256 * 1024 * 1204, 'Env' => $this->getBuildEnv($build), 'Image' => $build->getImageName(), 'Cmd' => ['buildapp'], 'Volumes' => ['/.composer/cache' => []]]); }
protected function configureContainer(Container $container, array $config) { if (!empty($config['name'])) { $container->setName($config['name']); } if (!empty($config['exposed_ports'])) { $ports = new PortCollection($config['exposed_ports']); $container->setExposedPorts($ports); } // TODO: Process Tmpfs configuration }
/** * @return Container|null */ public function getContainer() { if (null === $this->container && strlen($this->getContainerId()) > 0) { $container = new Container(); $container->setId($this->getContainerId()); $this->container = $container; } return $this->container; }
/** * Delete a container from docker server * * @param \Docker\Container $container * @param boolean $volumes * * @return \Docker\Manager\ContainerManager */ public function remove(Container $container, $volumes = false) { $response = $this->client->delete(['/containers/{id}?v={volumes}', ['id' => $container->getId(), 'v' => $volumes]]); if ($response->getStatusCode() !== "204") { throw UnexpectedStatusCodeException::fromResponse($response); } return $this; }
public function testInvalidConfigImageParsing() { $container = new Container(['Image' => 'postgres']); $this->setExpectedException('\\Docker\\Exception\\InvalidRepoTagException', 'Invalid repoTag: postgres'); $image = $container->getImage(); }
public function testExposeRandomPort() { $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/sleep', '1']]); $port = new Port('80/tcp'); $container->setExposedPorts($port); $manager = $this->getManager(); $manager->create($container); $manager->start($container, ['PortBindings' => $port->toSpec()]); $this->assertInternalType('integer', $container->getMappedPort(80)->getHostPort()); }
public function testInvalidContainerNameTwo() { $container = new Container(); $this->setExpectedException('Exception', 'Name was not correctly formatted.'); $container->setName('Foo!'); }
public function __construct(Build $build) { parent::__construct(['Image' => $build->getImageName('yuhao'), 'Cmd' => ['yuhao.sh'], 'Env' => ['SSH_URL=' . $build->getProject()->getGitUrl(), 'REF=' . $build->getRef(), 'IS_PULL_REQUEST=' . ($build->isPullRequest() ? 1 : 0)]]); }