public function denormalize($data, $class, $format = null, array $context = []) { if (empty($data)) { return null; } if (isset($data->{'$ref'})) { return new Reference($data->{'$ref'}, $context['rootSchema'] ?: null); } $object = new \Docker\API\Model\PortBinding(); if (!isset($context['rootSchema'])) { $context['rootSchema'] = $object; } if (isset($data->{'HostPort'})) { $object->setHostPort($data->{'HostPort'}); } if (isset($data->{'HostIp'})) { $object->setHostIp($data->{'HostIp'}); } return $object; }
/** * @return string * @throws ContainerException */ public function start() { try { $containerConfig = new \Docker\API\Model\ContainerConfig(); $containerConfig->setImage($this->image); $hostConfig = new \Docker\API\Model\HostConfig(); $mapPorts = new \ArrayObject(); $exposedPorts = new \ArrayObject(); foreach ($this->ports as $from => $to) { $exposedPorts[$from] = new \stdClass(); $hostPortBinding = new \Docker\API\Model\PortBinding(); list($host, $port) = explode(':', $to); $hostPortBinding->setHostIp($host); if (!empty($port)) { $hostPortBinding->setHostPort($port); } $mapPorts[$from] = [$hostPortBinding]; } $containerConfig->setExposedPorts($exposedPorts); $hostConfig->setPortBindings($mapPorts); $hostConfig->setLinks($this->links); $hostConfig->setBinds($this->volumes); $containerConfig->setHostConfig($hostConfig); $envs = []; foreach ($this->envs as $k => $v) { $envs[] = $k . "=" . $v; } $containerConfig->setEnv($envs); $containerCreateResult = $this->containerManager->create($containerConfig); $this->containerId = $containerCreateResult->getId(); $this->containerManager->start($this->containerId); return $this->containerId; } catch (ClientErrorException $e) { throw new ContainerException($e->getResponse()->getBody()->getContents(), $e->getCode(), $e); } catch (ServerErrorException $e) { throw new ContainerException($e->getResponse()->getBody()->getContents(), $e->getCode(), $e); } catch (\Exception $e) { throw new ContainerException($e->getMessage(), $e->getCode(), $e); } }