/**
  * Returns an array with Container objects on the system
  *
  * @param int $apiPort Network port to use in API call
  *
  * @return array $objects An array with Container objects
  *
  */
 public static function getContainers($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;
     }
     foreach ($data as $item) {
         $container = new OMVModuleDockerContainer($item->Id, $data, $apiPort);
         $ports = "";
         foreach ($container->getPorts() as $exposedport => $hostports) {
             if ($hostports) {
                 foreach ($hostports as $hostport) {
                     $ports .= $hostport["HostIp"] . ":" . $hostport["HostPort"] . "->" . $exposedport . ", ";
                 }
             } else {
                 $ports .= $exposedport . ", ";
             }
         }
         $image = OMVModuleDockerUtil::getImage(substr($container->getImageId(), 0, 12), $apiPort);
         $ports = rtrim($ports, ", ");
         $obj = array("id" => $container->getId(), "image" => $container->getImage(), "command" => $container->getCommand(), "created" => $container->getCreated(), "state" => $container->getState(), "status" => $container->getStatus(), "name" => $container->getName(), "privileged" => $container->getPrivileged(), "restartpolicy" => $container->getRestartPolicy(), "networkmode" => ucfirst($container->getNetworkMode()), "envvars" => $image->getEnvVars(), "cenvvars" => $container->getEnvironmentVariables(), "exposedports" => $image->getPorts(), "portbindings" => $container->getPortBindings(), "bindmounts" => $container->getBindMounts(), "ports" => $ports, "hasmounts" => $container->hasMounts(), "volumesfrom" => $container->getVolumesFrom(), "hostname" => $container->getHostName(), "timesync" => $container->syncsTime(), "imagevolumes" => $image->getVolumes());
         array_push($objects, $obj);
     }
     return $objects;
 }
Ejemplo n.º 2
0
 /**
  * Returns an array with Container objects on the system
  *
  * @return array $objects An array with Container objects
  *
  */
 public static function getContainers($apiPort)
 {
     $objects = array();
     $curl = curl_init();
     curl_setopt_array($curl, array(CURLOPT_RETURNTRANSFER => 1, CURLOPT_TIMEOUT => 30, CURLOPT_CONNECTTIMEOUT => 5));
     $url = "http://localhost:" . $apiPort . "/containers/json?all=1";
     curl_setopt($curl, CURLOPT_URL, $url);
     if (!($response = curl_exec($curl))) {
         throw new OMVModuleDockerException('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
     }
     curl_close($curl);
     $data = array();
     foreach (json_decode($response) as $item) {
         $data[substr($item->Id, 0, 12)] = $item;
     }
     foreach ($data as $item) {
         $container = new OMVModuleDockerContainer($item->Id, $data, $apiPort);
         $ports = "";
         foreach ($container->getPorts() as $exposedport => $hostports) {
             if ($hostports) {
                 foreach ($hostports as $hostport) {
                     $ports .= $hostport["HostIp"] . ":" . $hostport["HostPort"] . "->" . $exposedport . ", ";
                 }
             } else {
                 $ports .= $exposedport . ", ";
             }
         }
         $image = OMVModuleDockerUtil::getImage(substr($container->getImageId(), 0, 12), $apiPort);
         $exposedPorts = $image->getPorts();
         $envvars = $image->getEnvVars();
         $ports = rtrim($ports, ", ");
         $obj = array("id" => $container->getId(), "image" => $container->getImage(), "command" => $container->getCommand(), "created" => $container->getCreated(), "state" => $container->getState(), "status" => $container->getStatus(), "name" => $container->getName(), "privileged" => $container->getPrivileged(), "restartpolicy" => $container->getRestartPolicy(), "networkmode" => ucfirst($container->getNetworkMode()), "envvars" => $envvars, "cenvvars" => $container->getEnvironmentVariables(), "exposedports" => $exposedPorts, "portbindings" => $container->getPortBindings(), "bindmounts" => $container->getBindMounts(), "ports" => $ports);
         array_push($objects, $obj);
     }
     return $objects;
 }