Menu="OtherSettings"
Title="Docker"
Icon="dynamix.docker.manager.png"
Tag="docker"
---
<?PHP
/* Copyright 2005-2017, Lime Technology
 * Copyright 2014-2017, Guilherme Jardim, Eric Schultz, Jon Panozzo.
 *
 * Additional updates by Bergware International (July 2017)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */
?>
<?
// Add the Docker JSON client
require_once "$docroot/plugins/dynamix.docker.manager/include/DockerClient.php";

$docker          = new DockerClient();
$DockerUpdate    = new DockerUpdate();
$DockerTemplates = new DockerTemplates();
$DockerStopped   = pgrep('dockerd')===false;

// Check for nodatacow flag on Docker file; display warning
$realfile = $dockercfg['DOCKER_IMAGE_FILE'];
if (file_exists($realfile)) {
  $realfile = transpose_user_path($realfile);

  if (exec("stat -c %T -f " . escapeshellarg($realfile)) == "btrfs") {
    if (shell_exec("lsattr " . escapeshellarg($realfile) . " | grep \"\\-C\"") == "") {
      echo '<p class="notice">Your existing Docker image file needs to be recreated due to an issue from an earlier beta of unRAID 6.  Failure to do so may result in your docker image suffering corruption at a later time.  Please do this NOW!</p>';
    }
  }
}
unset($custom);
exec("ls --indicator-style=none /sys/class/net|grep -P '^br[0-9]'",$custom);
exec("ls --indicator-style=none /sys/class/net|grep -P '^(bond|eth)[0-9]'",$other);
foreach ($other as $network) {
  if (substr($network,0,4)=='bond') {
    $br = str_replace('bond','br',$network);
    if (!in_array($br,$custom)) $custom[] = $network;
  } else {
    $br = str_replace('eth','br',$network);
    $bond = str_replace('eth','bond',$network);
    if (!in_array($br,$custom) && !in_array($bond,$custom)) $custom[] = $network;
  }
}
$include = [];
$include6 = [];
foreach ($custom as $network) {
  if ($route = exec("ip -4 route show dev $network|grep -Po '^[12]\S+'")) $include[$network] = $route;
  if ($route6 = exec("ip -6 route show dev $network|grep -Po '^[1-9]\S+'")) $include6[$network] = $route6;
}
?>
<link type="text/css" rel="stylesheet" href="/webGui/styles/jquery.filetree.css">
<link type="text/css" rel="stylesheet" href="/webGui/styles/jquery.switchbutton.css">
<style>
  .errortext{color:#EF3D47;display:none;}
  .fileTree{width:305px;max-height:150px;overflow:scroll;position:absolute;z-index:100;display:none;}
  .basic{display: block;}
  .advanced{display:none;white-space: nowrap;}
</style>
<span class="status"><input type="checkbox" class="advancedview"></span>

<form markdown="1" id="settingsForm" name="settingsForm" method="POST" action="/update.php" target="progressFrame">
<input type="hidden" name="#file" value="<?=$docker_cfgfile?>">
<input type="hidden" name="#command" value="/plugins/dynamix/scripts/emhttpd_update">
Enable Docker:
: <select id="DOCKER_ENABLED" name="DOCKER_ENABLED" class="narrow">
  <?=mk_option($dockercfg['DOCKER_ENABLED'], 'no', 'No')?>
  <?=mk_option($dockercfg['DOCKER_ENABLED'], 'yes', 'Yes')?>
  </select>
  <?if ($var['fsState'] != "Started"):?>
  <span id="arraystopped"><i class="fa fa-warning icon warning"></i> <?=($dockercfg['DOCKER_ENABLED']=='yes')?'Docker will be available after Array is Started':'Apply to activate Docker after Array is Started'?></span>
  <?elseif (!is_dir(dirname($dockercfg['DOCKER_IMAGE_FILE'])) || !is_dir($dockercfg['DOCKER_APP_CONFIG_PATH'])):?>
  <span class="basic" style="display:inline"><i class="fa fa-warning icon warning"></i> One or more paths do not exist (<a href="#" onclick="$('.advancedview').switchButton('option','checked',true); return false">view</a>)</span>
  <?endif;?>

> Before you can start the Docker service for the first time, please specify an image file for Docker to install to.
>
> Once started, Docker will always automatically start after the array has been started.

<?if ($DockerStopped):?>
<div markdown="1" class="advanced">
Docker vdisk size:
: <input id="DOCKER_IMAGE_SIZE" type="number" name="DOCKER_IMAGE_SIZE" value="<?=$dockercfg['DOCKER_IMAGE_SIZE']?>" style="width:50px;" required="required">GB <span id="SIZE_ERROR" class="errortext"></span>

> If the system needs to create a new docker image file, this is the default size to use specified in GB.
>
> To resize an existing image file, specify the new size here.  Next time the Docker service is started the file (and file system) will increased to the new size (but never decreased).

Docker storage location:
: <input id="DOCKER_IMAGE_FILE" type="text" name="DOCKER_IMAGE_FILE" value="<?=$dockercfg['DOCKER_IMAGE_FILE']?>" placeholder="e.g. /mnt/disk1/docker.img" data-pickcloseonfile="true" data-pickfilter="img" data-pickroot="/mnt/" data-pickfolders="true" required="required">
  <?if (file_exists($dockercfg['DOCKER_IMAGE_FILE'])):?>
  <span id="deletePanel"><label><input type="checkbox" id="deleteCheckbox"> Delete Image File</label></span>
  <?endif;?>
  <?if ($var['fsState'] != "Started"):?>
  <span><i class="fa fa-warning icon warning"></i> Modify with caution: unable to validate path until Array is Started</span>
  <?elseif (!is_dir(dirname($dockercfg['DOCKER_IMAGE_FILE']))):?>
  <span><i class="fa fa-warning icon warning"></i> Path does not exist</span>
  <?endif;?><span id="IMAGE_ERROR" class="errortext"></span>

> You must specify an image file for Docker.  The system will automatically create this file when the Docker service is first started.

Docker LOG rotation:
: <select name="DOCKER_LOG_ROTATION" class="narrow" onchange="showLogOptions(this.value)">
  <?=mk_option($dockercfg['DOCKER_LOG_ROTATION'], 'no', 'Disabled')?>
  <?=mk_option($dockercfg['DOCKER_LOG_ROTATION'], 'yes', 'Enabled')?>
  </select>

> By default LOG rotation is disabled and will create a single LOG file of unlimited size.
>
> Enable LOG rotation to limit the size of the LOG file and specify the number of files to keep in the rotation scheme.

<div markdown="1" id="DOCKER_LOG_OPTIONS" style="display:none">
Docker LOG maximum file size:
: <select name="DOCKER_LOG_SIZE" class="narrow">
  <?=mk_option($dockercfg['DOCKER_LOG_SIZE'], '10m', '10 MB')?>
  <?=mk_option($dockercfg['DOCKER_LOG_SIZE'], '20m', '20 MB')?>
  <?=mk_option($dockercfg['DOCKER_LOG_SIZE'], '50m', '50 MB')?>
  <?=mk_option($dockercfg['DOCKER_LOG_SIZE'], '100m', '100 MB')?>
  <?=mk_option($dockercfg['DOCKER_LOG_SIZE'], '500m', '500 MB')?>
  <?=mk_option($dockercfg['DOCKER_LOG_SIZE'], '1g', '1000 MB')?>
  </select>

Docker LOG number of files:
: <select name="DOCKER_LOG_FILES" class="narrow">
  <?=mk_option($dockercfg['DOCKER_LOG_FILES'], '1', '1')?>
  <?=mk_option($dockercfg['DOCKER_LOG_FILES'], '2', '2')?>
  <?=mk_option($dockercfg['DOCKER_LOG_FILES'], '3', '3')?>
  </select>
</div>

</div>
<?else: /* DOCKER STARTED */?>
<div markdown="1" class="advanced">
Docker version:
: <?$arrInfo = $docker->getInfo(); echo $arrInfo['Version']?>

> This is the docker version.

Docker storage location:
: <?=$dockercfg['DOCKER_IMAGE_FILE']?>

> This is the docker volume.

Docker LOG rotation:
: <?=$dockercfg['DOCKER_LOG_ROTATION']=='yes'?'Enabled':'Disabled'?>

> By default a single unlimited LOG file is created. Otherwise LOG file size and number of files are limited when LOG rotation is enabled.

</div>
<?endif;?>
<div markdown="1" class="advanced">
Default appdata storage location:
: <input id="DOCKER_APP_CONFIG_PATH" type="text" name="DOCKER_APP_CONFIG_PATH" value="<?=$dockercfg['DOCKER_APP_CONFIG_PATH']?>" placeholder="e.g. /mnt/user/appdata" data-pickfilter="HIDE_FILES_FILTER" data-pickroot="<?=(is_dir('/mnt/user/') ? '/mnt/user/' : '/mnt/')?>" data-pickfolders="true">
  <?if ($var['fsState'] != "Started"):?>
  <span><i class="fa fa-warning icon warning"></i> Modify with caution: unable to validate path until Array is Started</span>
  <?elseif (!is_dir($dockercfg['DOCKER_APP_CONFIG_PATH'])):?>
  <span><i class="fa fa-warning icon warning"></i> Path does not exist</span>
  <?endif;?>

> You can specify a folder to automatically generate and store subfolders containing configuration files for each Docker app (via the /config mapped volume).
>
> Only used when adding new Docker apps.  Editing existing Docker apps will not be affected by this setting.

<!--
Auto-map user shares to containers as /unraid:
: <select id="DOCKER_APP_UNRAID_PATH" name="DOCKER_APP_UNRAID_PATH" class="narrow">
  <?=mk_option($dockercfg['DOCKER_APP_UNRAID_PATH'], ($dockercfg['DOCKER_APP_UNRAID_PATH'] != '' ? $dockercfg['DOCKER_APP_UNRAID_PATH'] : '/mnt/user'), 'Yes')?>
  <?=mk_option($dockercfg['DOCKER_APP_UNRAID_PATH'], '', 'No')?>
  </select>

> You can expose all of your user shares (/mnt/user) to a folder named /unraid within Docker containers.
>
> Only used when adding new Docker apps.  Editing existing Docker apps will not be affected by this setting.
-->
</div>
<div markdown="1" class="advanced">
Template Authoring Mode:
: <select id="DOCKER_AUTHORING_MODE" name="DOCKER_AUTHORING_MODE" class="narrow">
  <?=mk_option($dockercfg['DOCKER_AUTHORING_MODE'], 'no', 'No')?>
  <?=mk_option($dockercfg['DOCKER_AUTHORING_MODE'], 'yes', 'Yes')?>
  </select>

> If set to **Yes**, when creating/editing containers the interface will be present with some extra fields related to template authoring.

</div>
<div markdown="1" class="advanced">
<?foreach ($include as $network => $route):?>
<?$docker_dhcp = 'DOCKER_DHCP_'.strtoupper(str_replace('.','_',$network))?>

<?if ($DockerStopped):?>
DHCPv4 pool of custom network <?=$network?> (optional):
: <input type="text" name="<?=$docker_dhcp?>" value="<?=$dockercfg[$docker_dhcp]?>" class="narrow">Subnet: <span id="<?=$docker_dhcp?>"><?=$route?></span>
<?elseif ($dockercfg[$docker_dhcp]):?>
DHCPv4 pool of custom network <?=$network?>:
: <?=$dockercfg[$docker_dhcp]?>
<?endif;?>

<?endforeach;?>
> Enter a pool range within the allocated subnet which is used for DHCPv4 assignments by Docker. E.g. 192.168.1.128/25

<?foreach ($include6 as $network => $route):?>
<?$docker_dhcp6 = 'DOCKER_DHCP6_'.strtoupper(str_replace('.','_',$network))?>

<?if ($DockerStopped):?>
DHCPv6 pool of custom network <?=$network?> (optional):
: <input type="text" name="<?=$docker_dhcp6?>" value="<?=$dockercfg[$docker_dhcp6]?>" class="narrow">Subnet: <span id="<?=$docker_dhcp6?>"><?=$route?></span>
<?elseif ($dockercfg[$docker_dhcp6]):?>
DHCPv6 pool of custom network <?=$network?>:
: <?=$dockercfg[$docker_dhcp6]?>
<?endif;?>

> Enter a pool range within the allocated subnet which is used for DHCPv6 assignments by Docker. E.g. 2a02:abcd:9ef5:100:1::/80
<?endforeach;?>
</div>
&nbsp;
: <input id="applyBtn" type="button" value="Apply"><input type="button" value="Done" onclick="done()">
</form>

<form id="removeForm" method="POST" action="/update.php" target="progressFrame">
  <input type="hidden" name="#command" value="/plugins/dynamix.docker.manager/scripts/docker_rm">
</form>

<?if (!$DockerStopped):?>
<div markdown="1" class="advanced">
<div id="title"><span class="left"><i class="title fa fa-address-card-o"></i>Docker volume info</span></div>
btrfs filesystem show:
: <?="<pre>".shell_exec("btrfs filesystem show /var/lib/docker")."</pre>"?>

<form markdown="1" method="POST" action="/update.php" target="progressFrame">
<?exec("$docroot/webGui/scripts/btrfs_scrub status /var/lib/docker", $scrub_status, $retval)?>
btrfs scrub status:
: <?="<pre>".implode("\n", $scrub_status)."</pre>"?>

<?if ($retval != 0):?>
<input type="hidden" name="#command" value="/webGui/scripts/btrfs_scrub">
<input type="hidden" name="#arg[1]" value="start">
<input type="hidden" name="#arg[2]" value="/var/lib/docker">
<input type="hidden" name="#arg[3]" value="-r">
&nbsp;
: <input type="submit" value="Scrub"><label><input type="checkbox" name="#arg[3]" value=""> Correct file system errors</label>

> **Scrub** runs the *btrfs scrub* program to check file system integrity.
>
> If repair is needed you should check the *Correct file system errors* and run a second Scrub pass; this will permit *btrfs scrub* to fix the file system.

<?else:?>
<input type="hidden" name="#command" value="/webGui/scripts/btrfs_scrub">
<input type="hidden" name="#arg[1]" value="cancel">
<input type="hidden" name="#arg[2]" value="/var/lib/docker">
&nbsp;
: <input type="button" value="Refresh" onclick="refresh()"><input type="submit" value="Cancel"> *Running*

> **Cancel** will cancel the Scrub operation in progress.

</form>
<?endif;?>
</div>
<?endif;?>

<script src="/webGui/javascript/jquery.filetree.js"></script>
<script src="/webGui/javascript/jquery.switchbutton.js"></script>
<script>
function ip2int(ip) {
  return ip.split('.').reduce(function(ipInt,octet){return (ipInt<<8)+parseInt(octet,10)},0)>>>0;
}
function checkDHCPv4() {
  var good = true;
  $('#settingsForm').find('input[name^="DOCKER_DHCP_"]').each(function(){
    if ($(this).val()) {
      var id = $(this).attr('name');
      var pool = $(this).val().split('/');
      var base = $('#'+id).text().split('/');
      if (good && typeof(pool[1])=='undefined') {good = false; swal('Missing subnet size','Pool subnet size is not defined','error');}
      if (good && pool[1]<=base[1]) {good = false; swal('Invalid subnet size','Pool subnet size is too large','error');}
      if (typeof(pool[1])=='undefined') pool[1] = 0;
      if (typeof(base[1])=='undefined') base[1] = 32;
      var toppool = ip2int(pool[0]);
      var topbase = ip2int(base[0]);
      var endpool = toppool+2**(32-pool[1]);
      var endbase = topbase+2**(32-base[1]);
      if (good && (toppool < topbase || endpool > endbase)) {good = false; swal('Invalid pool address','Pool address is out of range','error');}
    }
  });
  return good;
}
function checkDHCPv6() {
  var good = true;
  $('#settingsForm').find('input[name^="DOCKER_DHCP6_"]').each(function(){
    if ($(this).val()) {
      var id = $(this).attr('name');
      var pool = $(this).val().split('/');
      var base = $('#'+id).text().split('/');
      if (good && typeof(pool[1])=='undefined') {good = false; swal('Missing subnet size','Pool subnet size is not defined','error');}
      if (good && pool[1]<=base[1]) {good = false; swal('Invalid subnet size','Pool subnet size is too large','error');}
    }
  });
  return good;
}
function showLogOptions(log) {
  if (log == 'no') {
    $('#DOCKER_LOG_OPTIONS').hide('slow');
  } else {
    $('#DOCKER_LOG_OPTIONS').show('slow');
  }
}
$(function() {
  $("#applyBtn").click(function(){
    if (!checkDHCPv4() || !checkDHCPv6()) return;
    if ($("#deleteCheckbox").length && $("#deleteCheckbox").is(":checked")) {
      $("#removeForm").submit();
      return;
    }
    if ($("#DOCKER_IMAGE_SIZE").length && $("#DOCKER_IMAGE_FILE").length) {
      var isError = false;
      var size = $("#DOCKER_IMAGE_SIZE").val();
      var target = $("#SIZE_ERROR");
      if (! $.isNumeric( size )){
        target.fadeIn().html('Error: value must be a number.');
        isError = true;
      } else {
        target.fadeOut();
      }
      var image = $("#DOCKER_IMAGE_FILE").val();

      target = $("#IMAGE_ERROR");
      $.getJSON( "/plugins/dynamix.docker.manager/include/UpdateConfig.php?is_dir=" + image).done(function( json ) {
        if (json.is_dir === true){
          target.fadeIn().html('Error: must be a file; directory provided.');
          isError = true;
        } else {
          target.fadeOut();
        }

        if (!isError) {$("#settingsForm").submit();}
      });
      return;
    }

    $("#settingsForm").submit();
  });

  $("#DOCKER_ENABLED").change(function changeService() {
    if ($(this).val()=='yes') {
      $('#arraystopped').fadeIn('slow');
    } else {
      $('#arraystopped').fadeOut('fast');
    }
  });
  if ($("#DOCKER_ENABLED").val()!='yes') $('#arraystopped').hide();

  if ($("#DOCKER_IMAGE_FILE").length) {
    $("#DOCKER_IMAGE_FILE").on("input change", function(){
      $("#IMAGE_ERROR").fadeOut();
      $("#applyBtn").prop("disabled", false);
<?if (file_exists($dockercfg['DOCKER_IMAGE_FILE'])):?>
      if ($(this).val() != "<?=$dockercfg['DOCKER_IMAGE_FILE']?>") {
        $("#deleteCheckbox").prop("disabled", true).attr("checked", false);
        $("#deletePanel").fadeOut();
      } else {
        $("#deleteCheckbox").attr("checked", false).prop("disabled", false);
        $("#deletePanel").fadeIn();
      }
<?endif;?>
    });

<?if ($var['fsState'] == "Started"):?>
    $("#DOCKER_IMAGE_FILE").fileTreeAttach(null, null, function(folder) {
      $("#DOCKER_IMAGE_FILE").val(folder + 'docker.img').change();
    });
<?endif;?>

    $("#deleteCheckbox").change(function(){
      var checked = $(this).is(":checked");
      $("#DOCKER_ENABLED").prop("disabled", checked).val('no');
      $("#DOCKER_IMAGE_SIZE").prop("disabled", checked);
      $("#DOCKER_IMAGE_FILE").prop("disabled", checked).val("<?=$dockercfg['DOCKER_IMAGE_FILE']?>");
      $("#DOCKER_APP_CONFIG_PATH").prop("disabled", checked);
      $("#DOCKER_APP_UNRAID_PATH").prop("disabled", checked);
      $("#applyBtn").val(checked ? "Delete" : "Apply").removeAttr('disabled');
    });
  }

  if ($.cookie('dockersettings_view_mode') == 'advanced') {
    $('.advanced').show();
    $('.basic').hide();
  }
  $('.advancedview').switchButton({
    labels_placement: "left",
    on_label: 'Advanced View',
    off_label: 'Basic View',
    checked: $.cookie('dockersettings_view_mode') == 'advanced'
  });
  $('.advancedview').change(function () {
    $('.advanced').toggle('slow');
    $('.basic').toggle('slow');
    $.cookie('dockersettings_view_mode', $('.advancedview').is(':checked') ? 'advanced' : 'basic', { expires: 3650 });
  });

  showStatus('pid','dockerd');
<?if ($DockerStopped):?>
  showLogOptions(document.settingsForm.DOCKER_LOG_ROTATION.value);
<?endif;?>
<?if ($var['fsState'] == "Started"):?>
  $("#DOCKER_APP_CONFIG_PATH").fileTreeAttach();
<?endif;?>
});
</script>
