<?php
require __DIR__ . '/vendor/autoload.php';
use Docker\API\Endpoint\ImageLoad;
use Docker\Docker;
$docker = Docker::create();
$archive = fopen(__DIR__ . '/saved-image.tar', 'rb');
if ($archive === false) {
throw new RuntimeException('Could not open the image archive');
}
$body = null;
try {
$response = $docker->executeRawEndpoint(new ImageLoad($archive, ['quiet' => true]));
$body = $response->getBody();
if ($response->getStatusCode() !== 200) {
throw new RuntimeException('Image load failed: HTTP '.$response->getStatusCode());
}
// The quiet response is finite; it can still contain several JSON lines.
foreach (preg_split('/\r?\n/', $body->getContents()) as $line) {
if (trim($line) === '') {
continue;
}
$frame = json_decode($line, true, 512, JSON_THROW_ON_ERROR);
if (isset($frame['error']) || isset($frame['errorDetail']['message'])) {
throw new RuntimeException($frame['error'] ?? $frame['errorDetail']['message']);
}
echo $frame['stream'] ?? '';
}
} finally {
$body?->close();
if (is_resource($archive)) {
fclose($archive);
}
}