Skip to content

Commit 46a7784

Browse files
committed
Add container export example
1 parent 69afb9e commit 46a7784

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

examples/export.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
// this example shows how the containerExport() call returns a TAR stream
3+
// and how we it can be piped into a output tar file.
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
use React\EventLoop\StreamSelectLoop;
8+
use Clue\React\Docker\Factory;
9+
use React\Stream\Stream;
10+
11+
$container = isset($argv[1]) ? $argv[1] : 'asd';
12+
$target = isset($argv[2]) ? $argv[2] : ($container . '.tar');
13+
echo 'Exporting whole container "' . $container . '" to "' . $target .'" (pass as arguments to this example)' . PHP_EOL;
14+
15+
$loop = new StreamSelectLoop();
16+
17+
$factory = new Factory($loop);
18+
$client = $factory->createClient();
19+
20+
$stream = $client->containerExportStream($container);
21+
22+
$stream->on('error', function ($e = null) {
23+
// will be called if the container is invalid/does not exist
24+
echo 'ERROR requesting stream' . PHP_EOL . $e;
25+
});
26+
27+
$out = new Stream(fopen($target, 'w'), $loop);
28+
$stream->pipe($out);
29+
30+
$loop->run();

0 commit comments

Comments
 (0)