Skip to content
Snippets Groups Projects
volume.php 787 B
<?php


function readContainerConfig() {
  try {
    $output = [];
    exec('basename "$(cat /proc/1/cpuset)"', $output);
    $id = reset($output);
    $output = [];
    exec('docker container inspect ' . $id, $output);
    return json_decode(implode('', $output), TRUE)[0];
  }
  catch (\Exception $ex) {
    // Ignore.
  }
  return [];
}

function getDockerMountSource($args) {
  $currentDir = getcwd();
  $destDir = (empty($args[1])) ? $currentDir : $args[1];

  $container = readContainerConfig();
  foreach ($container['Mounts'] as $mount) {
    if (strpos($currentDir, $mount['Destination']) === 0) {
      return $mount['Source'] . ':' . (empty($args[1]) ? $mount['Destination'] : $destDir);
    }
  }
  return $currentDir . ':' . $destDir;
}

print(getDockerMountSource($argv));