/**
  * Returns a single container from it's ID
  *
  * @param string $id      The ID of the container to retrieve
  * @param int    $apiPort Network port to use in API call
  *
  * @return OMVModuleDockerContainer $container A single container object
  *
  */
 public static function getContainer($id, $apiPort)
 {
     $objects = array();
     $url = "http://localhost:" . $apiPort . "/containers/json?all=1";
     $response = OMVModuleDockerUtil::doApiCall($url);
     $data = array();
     foreach (json_decode($response) as $item) {
         $data[substr($item->Id, 0, 12)] = $item;
     }
     return new OMVModuleDockerContainer($data[$id]->Id, $data, $apiPort);
 }
 /**
  * Constructor. The container will be updated with all associated
  * properties from commandline.
  *
  * @param string $id      Id of the new image
  * @param array  $data    An associative array with all containers
  * @param imt    $apiPort An associative array with all containers
  *
  * @return void
  * @access public
  */
 public function __construct($id, $data, $apiPort)
 {
     $this->_id = $id;
     $now = date("c");
     $item = $data[substr($id, 0, 12)];
     $this->_status = $item->Status;
     $this->_command = $item->Command;
     $this->_created = OMVModuleDockerUtil::getWhen($now, date("c", $item->Created)) . " ago";
     $url = "http://localhost:" . $apiPort . "/containers/{$id}/json";
     $response = OMVModuleDockerUtil::doApiCall($url);
     $containerData = json_decode($response);
     $this->_image = $containerData->Config->Image;
     $this->_state = "running";
     if ($containerData->State->Running) {
         $this->_state = "running";
     } elseif ($containerData->State->Dead || $containerData->State->ExitCode !== 0) {
         $this->_state = "dead";
     } elseif ($containerData->State->ExitCode === 0 && strcmp($containerData->State->Error, "") === 0) {
         $this->_state = "stopped";
     }
     $this->_ports = array();
     if (isset($containerData->NetworkSettings->Ports)) {
         foreach ($containerData->NetworkSettings->Ports as $exposedport => $hostports) {
             if ($hostports) {
                 $this->_ports[$exposedport] = array();
                 foreach ($hostports as $hostport) {
                     $tmparray = array("HostIp" => $hostport->HostIp, "HostPort" => $hostport->HostPort);
                     array_push($this->_ports[$exposedport], $tmparray);
                 }
             } else {
                 $this->_ports[$exposedport] = null;
             }
         }
     }
     $this->_networkMode = $containerData->HostConfig->NetworkMode;
     if (strcmp($this->_networkMode, "default") === 0) {
         $this->_networkMode = "bridge";
     }
     $this->_privileged = $containerData->HostConfig->Privileged;
     $this->_restartPolicy = $containerData->HostConfig->RestartPolicy->Name;
     $this->_envVars = array();
     if (isset($containerData->Config->Env)) {
         foreach ($containerData->Config->Env as $eVar) {
             $eVarAry = explode("=", $eVar);
             $this->_envVars[$eVarAry[0]] = $eVarAry[1];
         }
     }
     $this->_imageId = substr($containerData->Image, 7);
     $this->_portBindings = array();
     if (isset($containerData->HostConfig->PortBindings)) {
         foreach ($containerData->HostConfig->PortBindings as $containerPort => $mappings) {
             foreach ($mappings as $mapping) {
                 array_push($this->_portBindings, array("containerportstring" => $containerPort, "containerportnr" => preg_split('/\\//', $containerPort)[0], "hostip" => $mapping->HostIp, "hostport" => $mapping->HostPort, "proto" => preg_split('/\\//', $containerPort)[1]));
             }
         }
     }
     $this->_bindMounts = array();
     $this->_timeSync = false;
     if (isset($containerData->HostConfig->Binds)) {
         foreach ($containerData->HostConfig->Binds as $bind) {
             if (strcmp($bind, "/etc/localtime:/etc/localtime:ro") === 0) {
                 $this->_timeSync = true;
                 continue;
             }
             $mode = "";
             if (isset(preg_split('/\\:/', $bind)[2])) {
                 $mode = preg_split('/\\:/', $bind)[2];
             }
             array_push($this->_bindMounts, array("from" => preg_split('/\\:/', $bind)[0], "to" => preg_split('/\\:/', $bind)[1], "mode" => $mode));
         }
     }
     if (isset($containerData->Mounts)) {
         foreach ($containerData->Mounts as $mount) {
             if (strcmp($mount->Mode, "") === 0 && isset($mount->Driver) && strcmp($mount->Driver, "local") === 0) {
                 array_push($this->_bindMounts, array("from" => $mount->Destination, "to" => "", "mode" => $mount->Mode));
             }
         }
     }
     $this->_names = ltrim($item->Names[0], "/");
     $this->_hasMounts = false;
     if (is_array($containerData->Mounts) && count($containerData->Mounts) > 0) {
         $this->_hasMounts = true;
     }
     $this->_volumesFrom = array();
     if (isset($containerData->HostConfig->VolumesFrom)) {
         foreach ($containerData->HostConfig->VolumesFrom as $volume) {
             array_push($this->_volumesFrom, array("from" => $volume));
         }
     }
     $this->_hostName = "";
     if (!(strcmp($containerData->Config->Hostname, "") === 0)) {
         $this->_hostName .= $containerData->Config->Hostname;
         if (!(strcmp($containerData->Config->Domainname, "") === 0)) {
             $this->_hostName .= "." . $containerData->Config->Domainname;
         }
     }
 }
 /**
  * Constructor. The image will be updated with all associated properties
  * from commandline.
  *
  * @param string $id      Id of the new container
  * @param array  $data    Associative array with Image data
  * @param int    $apiPort Network port used by API calls
  */
 public function __construct($id, $data, $apiPort)
 {
     $now = date("c");
     $this->_id = $id;
     $item = $data[substr($id, 0, 12)];
     if (isset($item->RepoTags)) {
         $this->_repository = preg_split('/\\:/', $item->RepoTags[0])[0];
         $this->_tag = preg_split('/\\:/', $item->RepoTags[0])[1];
     } else {
         $this->_repository = "none";
         $this->_tag = "none";
     }
     $this->_created = OMVModuleDockerUtil::getWhen($now, date("c", $item->Created)) . " ago";
     $this->_size = OMVModuleDockerUtil::bytesToSize($item->VirtualSize);
     $url = "http://localhost:" . $apiPort . "/images/{$id}/json";
     $response = OMVModuleDockerUtil::doApiCall($url);
     $imageData = json_decode($response);
     $this->_timestamp = date("U", $item->Created);
     $this->_ports = array();
     if (isset($imageData->Config->ExposedPorts)) {
         foreach ($imageData->Config->ExposedPorts as $exposedport => $hostports) {
             array_push($this->_ports, array("name" => $exposedport));
         }
     }
     $this->_envVars = array();
     if (isset($imageData->Config->Env)) {
         foreach ($imageData->Config->Env as $eVar) {
             $eVarAry = explode("=", $eVar);
             $this->_envVars[$eVarAry[0]] = $eVarAry[1];
         }
     }
     $this->_volumes = array();
     if (isset($imageData->Config->Volumes)) {
         foreach ($imageData->Config->Volumes as $key => $val) {
             array_push($this->_volumes, array($key));
         }
     }
 }