$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
// add translations
$_SERVER['REQUEST_URI'] = 'docker';
require_once "$docroot/webGui/include/Translations.php";
require_once "$docroot/plugins/dynamix.docker.manager/include/DockerClient.php";
require_once "$docroot/webGui/include/Helpers.php";
$DockerClient = new DockerClient();
$DockerTemplates = new DockerTemplates();
$containers = $DockerClient->getDockerContainers();
$images = $DockerClient->getDockerImages();
$user_prefs = $dockerManPaths['user-prefs'];
$autostart_file = $dockerManPaths['autostart-file'];
if (!$containers && !$images) {
echo "
| "._('No Docker containers installed')." |
";
return;
}
if (file_exists($user_prefs)) {
$prefs = parse_ini_file($user_prefs); $sort = [];
foreach ($containers as $ct) $sort[] = array_search($ct['Name'],$prefs) ?? 999;
array_multisort($sort,SORT_NUMERIC,$containers);
unset($sort);
}
// Read container info
$allInfo = $DockerTemplates->getAllInfo();
$docker = ['var docker=[];'];
$null = '0.0.0.0';
$menu = [];
$autostart = @file($autostart_file, FILE_IGNORE_NEW_LINES) ?: [];
$names = array_map('var_split', $autostart);
function my_lang_time($text) {
[$number, $text] = explode(' ',$text,2);
return sprintf(_("%s $text"),$number);
}
function my_lang_log($text) {
global $language;
if (isset($language['healthy'])) $text = str_replace('healthy',$language['healthy'],$text);
if (isset($language['Exited'])) $text = str_replace('Exited',$language['Exited'],$text);
if (strpos($text,'ago')!==false) {
[$t1,$t2] = explode(') ',$text);
return $t1.'): '.my_lang_time($t2);
}
return _(_($text),2);
}
foreach ($containers as $ct) {
$name = $ct['Name'];
$id = $ct['Id'];
$info = &$allInfo[$name];
$running = $info['running'] ? 1 : 0;
$paused = $info['paused'] ? 1 : 0;
$is_autostart = $info['autostart'] ? 'true':'false';
$updateStatus = substr($ct['NetworkMode'],-4)==':???' ? 2 : ($info['updated']=='true' ? 0 : ($info['updated']=='false' ? 1 : 3));
$template = $info['template'];
$shell = $info['shell'];
$webGui = html_entity_decode($info['url']);
$support = html_entity_decode($info['Support']);
$project = html_entity_decode($info['Project']);
$registry = html_entity_decode($info['registry']);
$menu[] = sprintf("addDockerContainerContext('%s','%s','%s',%s,%s,%s,%s,'%s','%s','%s','%s','%s','%s');", addslashes($name), addslashes($ct['ImageId']), addslashes($template), $running, $paused, $updateStatus, $is_autostart, addslashes($webGui), $shell, $id, addslashes($support), addslashes($project),addslashes($registry));
$docker[] = "docker.push({name:'$name',id:'$id',state:$running,pause:$paused,update:$updateStatus});";
$shape = $running ? ($paused ? 'pause' : 'play') : 'square';
$status = $running ? ($paused ? 'paused' : 'started') : 'stopped';
$color = $status=='started' ? 'green-text' : ($status=='paused' ? 'orange-text' : 'red-text');
$update = $updateStatus==1 ? 'blue-text' : '';
$icon = $info['icon'] ?: '/plugins/dynamix.docker.manager/images/question.png';
$image = substr($icon,-4)=='.png' ? "
" : (substr($icon,0,5)=='icon-' ? "" : "");
$wait = var_split($autostart[array_search($name,$names)],1);
$ports = [];
foreach ($ct['Ports'] as $port) {
$intern = $running ? ($ct['NetworkMode']=='host' ? $host : $port['IP']) : $null;
$extern = $running ? ($port['NAT'] ? $host : $intern) : $null;
$ports[] = sprintf('%s:%s/%s%s:%s', $intern, $port['PrivatePort'], strtoupper($port['Type']), $extern, $port['PublicPort']);
}
$paths = [];
$ct['Volumes'] = is_array($ct['Volumes']) ? $ct['Volumes'] : [];
foreach ($ct['Volumes'] as $mount) {
list($host_path,$container_path,$access_mode) = explode(':',$mount);
$paths[] = sprintf('%s%s', htmlspecialchars($container_path), $access_mode=='ro'?'long-arrow-left':'arrows-h', htmlspecialchars($host_path));
}
echo "";
if ($template) {
$appname = "".htmlspecialchars($name)."";
} else {
$appname = htmlspecialchars($name);
}
echo "$image$appname "._($status)."";
echo ""._('Container ID').": $id ";
if ($ct['BaseImage']) echo "".htmlspecialchars(${ct['BaseImage']})." ";
echo _('By').": ";
$registry = $info['registry'];
[$author,$version] = explode(':',$ct['Image']);
if ($registry) {
echo "".htmlspecialchars($author)."";
} else {
echo htmlspecialchars($author);
}
echo " | ";
switch ($updateStatus) {
case 0:
echo " "._('up-to-date')."";
echo "";
break;
case 1:
echo " "._('update ready')." ";
echo " "._('apply update')."";
break;
case 2:
echo " ".('rebuild ready')." ";
echo " "._('rebuilding')."";
break;
default:
echo " "._('not available')."";
echo "";
break;
}
echo " ".compress(_($version),12,0)." | ";
echo "{$ct['NetworkMode']} | ";
echo "".implode(' ',$ports)." | ";
echo "".implode(' ',$paths)." | ";
echo "0% ";
echo " 0 / 0 | ";
echo "";
echo ""._('wait')." | ";
echo " ";
echo htmlspecialchars(str_replace('Up',_('Uptime').':',my_lang_log($ct['Status'])))." "._('Created').": ".htmlspecialchars(my_lang_time($ct['Created']))." |
";
}
foreach ($images as $image) {
if (count($image['usedBy'])) continue;
$id = $image['Id'];
$menu[] = sprintf("addDockerImageContext('%s','%s');", $id, implode(',',$image['Tags']));
echo "";
echo " ("._('orphan image').") "._('stopped')."";
echo " | "._('Image ID').": $id ";
echo implode(', ',array_map('htmlspecialchars',$image['Tags']));
echo " | "._('Created')." ".htmlspecialchars(_($image['Created'],0))." |
";
}
echo "\0".implode($menu).implode($docker)."\0".(pgrep('rc.docker')!==false ? 1:0);
?>