Skip to content

Instantly share code, notes, and snippets.

@xgin
Last active May 6, 2016 05:15
Show Gist options
  • Save xgin/a2d62adb3bab69bb9179a0f624be37ae to your computer and use it in GitHub Desktop.
Save xgin/a2d62adb3bab69bb9179a0f624be37ae to your computer and use it in GitHub Desktop.
Track progress of image creation with docker-php library
<?php
$docker = new Docker\Docker();
$imageManager = $docker->getImageManager();
$params = [
'fromImage' => $this->getParam('name'),
'tag' => ('' === $this->getParam('tag')) ? 'latest' : $this->getParam('tag'),
];
$stream = $imageManager->create(null, $params, $imageManager::FETCH_STREAM);
$layerProgress = [];
$stream->onFrame(function (CreateImageInfo $createImageInfo) use (&$layerProgress) {
if ($createImageInfo->getStatus() == 'Pulling fs layer') {
$layerProgress[$createImageInfo->getId()] = [
'current' => 0,
'total' => 0,
];
} elseif ($createImageInfo->getStatus() == 'Downloading' &&
($details = $createImageInfo->getProgressDetail())) {
$layerProgress[$createImageInfo->getId()] = [
'current' => $details->getCurrent(),
'total' => $details->getTotal(),
];
$current = array_sum(array_column($layerProgress, 'current'));
$total = array_sum(array_column($layerProgress, 'total'));
if ($total) {
$this->setParam('current', round($current / static::MEGABYTE));
$this->setParam('total', round($total / static::MEGABYTE));
$this->updateProgress(round(100 * $current / $total));
}
}
});
$stream->wait();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment