Skip to content

Use new HTTP and Socket API and improve documentation and examples #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/vendor
/composer.lock
/vendor/
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ Once [installed](#install), you can use the following code to access the
Docker API of your local docker daemon:

```php
<?php

require __DIR__ . '/vendor/autoload.php';

$client = new Clue\React\Docker\Client();

$client->imageSearch('clue')->then(function (array $images) {
Expand Down Expand Up @@ -379,7 +383,7 @@ See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.

This project aims to run on any platform and thus does not require any PHP
extensions and supports running on legacy PHP 5.3 through current PHP 8+.
It's *highly recommended to use PHP 7+* for this project.
It's *highly recommended to use the latest supported PHP version* for this project.

## Tests

Expand All @@ -393,7 +397,7 @@ $ composer install
To run the test suite, go to the project root and run:

```bash
$ php vendor/bin/phpunit
$ vendor/bin/phpunit
```

## License
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
"php": ">=5.3",
"clue/json-stream": "^0.1",
"react/event-loop": "^1.2",
"react/http": "^1.4",
"react/http": "^1.5",
"react/promise": "^2.0 || ^1.1",
"react/promise-stream": "^1.0",
"react/socket": "^1.8",
"react/socket": "^1.9",
"react/stream": "^1.2",
"rize/uri-template": "^0.3"
},
Expand Down
13 changes: 4 additions & 9 deletions examples/archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,22 @@
// how it can be passed to a TAR decoder and how we can then pipe each
// individual file to the console output.

use Clue\CaretNotation\Encoder;
use Clue\React\Docker\Client;
use Clue\React\Tar\Decoder;
use React\Stream\ReadableStreamInterface;

require __DIR__ . '/../vendor/autoload.php';

$container = isset($argv[1]) ? $argv[1] : 'asd';
$path = isset($argv[2]) ? $argv[2] : '/etc/passwd';
echo 'Container "' . $container . '" dumping "' . $path . '" (pass as arguments to this example)' . PHP_EOL;

$client = new Client();
$client = new Clue\React\Docker\Client();

$stream = $client->containerArchiveStream($container, $path);

$tar = new Decoder();
$tar = new Clue\React\Tar\Decoder();

// use caret notation for any control characters except \t, \r and \n
$caret = new Encoder("\t\r\n");
$caret = new Clue\CaretNotation\Encoder("\t\r\n");

$tar->on('entry', function ($header, ReadableStreamInterface $file) use ($caret) {
$tar->on('entry', function ($header, React\Stream\ReadableStreamInterface $file) use ($caret) {
// write each entry to the console output
echo '########## ' . $caret->encode($header['filename']) . ' ##########' . PHP_EOL;
$file->on('data', function ($chunk) use ($caret) {
Expand Down
7 changes: 2 additions & 5 deletions examples/attach-stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@
// $ docker run -it --rm --name=foo busybox sh
// $ php examples/attach-stream.php foo

use Clue\CaretNotation\Encoder;
use Clue\React\Docker\Client;

require __DIR__ . '/../vendor/autoload.php';

$container = isset($argv[1]) ? $argv[1] : 'foo';
echo 'Dumping output of container "' . $container . '" (pass as argument to this example)' . PHP_EOL;

$client = new Client();
$client = new Clue\React\Docker\Client();

// use caret notation for any control characters except \t, \r and \n
$caret = new Encoder("\t\r\n");
$caret = new Clue\CaretNotation\Encoder("\t\r\n");

$stream = $client->containerAttachStream($container, true, true);
$stream->on('data', function ($data) use ($caret) {
Expand Down
3 changes: 1 addition & 2 deletions examples/benchmark-attach.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
//
// $ docker run -i --rm --log-driver=none busybox dd if=/dev/zero bs=1M count=1000 status=none | dd of=/dev/null

use Clue\React\Docker\Client;
use React\EventLoop\Loop;

require __DIR__ . '/../vendor/autoload.php';
Expand All @@ -27,7 +26,7 @@
$cmd = array_slice($argv, 2);
}

$client = new Client();
$client = new Clue\React\Docker\Client();

$client->containerCreate(array(
'Image' => $image,
Expand Down
3 changes: 1 addition & 2 deletions examples/benchmark-exec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
//
// $ docker exec foo dd if=/dev/zero bs=1M count=1000 | dd of=/dev/null

use Clue\React\Docker\Client;

require __DIR__ . '/../vendor/autoload.php';

Expand All @@ -29,7 +28,7 @@
$cmd = array_slice($argv, 2);
}

$client = new Client();
$client = new Clue\React\Docker\Client();

$client->execCreate($container, $cmd)->then(function ($info) use ($client) {
$stream = $client->execStartStream($info['Id'], true);
Expand Down
3 changes: 1 addition & 2 deletions examples/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
// this simple example displays all docker events that happen in the next 10s.
// try starting / removing a container in the meantime to see some output.

use Clue\React\Docker\Client;

require __DIR__ . '/../vendor/autoload.php';

$client = new Client();
$client = new Clue\React\Docker\Client();

// get a list of all events that happened up until this point
// expect this list to be limited to the last 64 (or so) events
Expand Down
7 changes: 2 additions & 5 deletions examples/exec-inspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

// this simple example executes a "sleep 2" within the given running container

use Clue\React\Buzz\Message\ResponseException;
use Clue\React\Docker\Client;

require __DIR__ . '/../vendor/autoload.php';

$container = 'asd';
Expand All @@ -18,7 +15,7 @@
$cmd = array_slice($argv, 2);
}

$client = new Client();
$client = new Clue\React\Docker\Client();

$client->execCreate($container, $cmd)->then(function ($info) use ($client) {
echo 'Created with info: ' . json_encode($info) . PHP_EOL;
Expand All @@ -38,7 +35,7 @@
}, function (Exception $e) {
echo 'ERROR: ' . $e->getMessage() . PHP_EOL;

if ($e instanceof ResponseException) {
if ($e instanceof React\Http\Message\ResponseException) {
echo 'Response: ' . $e->getResponse()->getBody() . PHP_EOL;
}
});
8 changes: 3 additions & 5 deletions examples/exec-stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
// this example executes some commands within the given running container and
// displays the streaming output as it happens.

use Clue\React\Docker\Client;
use React\EventLoop\Loop;
use React\Stream\WritableResourceStream;

require __DIR__ . '/../vendor/autoload.php';

Expand All @@ -24,10 +22,10 @@
$cmd = array_slice($argv, 2);
}

$client = new Client();
$client = new Clue\React\Docker\Client();

$out = new WritableResourceStream(STDOUT);
$stderr = new WritableResourceStream(STDERR);
$out = new React\Stream\WritableResourceStream(STDOUT);
$stderr = new React\Stream\WritableResourceStream(STDERR);

// unkown exit code by default
$exit = 1;
Expand Down
7 changes: 2 additions & 5 deletions examples/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
// this example shows how the containerExport() call returns a TAR stream
// and how we it can be piped into a output tar file.

use Clue\React\Docker\Client;
use React\Stream\WritableResourceStream;

require __DIR__ . '/../vendor/autoload.php';

if (DIRECTORY_SEPARATOR === '\\') {
Expand All @@ -16,7 +13,7 @@
$target = isset($argv[2]) ? $argv[2] : ($container . '.tar');
echo 'Exporting whole container "' . $container . '" to "' . $target .'" (pass as arguments to this example)' . PHP_EOL;

$client = new Client();
$client = new Clue\React\Docker\Client();

$stream = $client->containerExportStream($container);

Expand All @@ -25,5 +22,5 @@
echo 'ERROR requesting stream' . PHP_EOL . $e;
});

$out = new WritableResourceStream(fopen($target, 'w'));
$out = new React\Stream\WritableResourceStream(fopen($target, 'w'));
$stream->pipe($out);
4 changes: 1 addition & 3 deletions examples/info.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

// this simple example displays system wide information from Docker as a simple JSON

use Clue\React\Docker\Client;

require __DIR__ . '/../vendor/autoload.php';

$client = new Client();
$client = new Clue\React\Docker\Client();

$client->info()->then(function ($info) {
echo json_encode($info, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
Expand Down
7 changes: 2 additions & 5 deletions examples/logs-stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
// this example shows how the containerLogsStream() call can be used to get the logs of the given container.
// demonstrates the streaming logs API, which can be used to dump the logs as they arrive

use Clue\CaretNotation\Encoder;
use Clue\React\Docker\Client;

require __DIR__ . '/../vendor/autoload.php';

$container = isset($argv[1]) ? $argv[1] : 'asd';
echo 'Dumping logs (last 100 lines) of container "' . $container . '" (pass as argument to this example)' . PHP_EOL;

$client = new Client();
$client = new Clue\React\Docker\Client();

// use caret notation for any control characters except \t, \r and \n
$caret = new Encoder("\t\r\n");
$caret = new Clue\CaretNotation\Encoder("\t\r\n");

$stream = $client->containerLogsStream($container, true, true, true, 0, false, 100);
$stream->on('data', function ($data) use ($caret) {
Expand Down
4 changes: 2 additions & 2 deletions examples/logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
$container = isset($argv[1]) ? $argv[1] : 'foo';
echo 'Dumping logs (last 100 lines) of container "' . $container . '" (pass as argument to this example)' . PHP_EOL;

$client = new Client();
$client = new Clue\React\Docker\Client();

$client->containerLogs($container, false, true, true, 0, false, 100)->then(
function ($logs) {
echo 'Received the following logs:' . PHP_EOL;

// escape control characters (dumping logs of vi/nano etc.)
$caret = new Encoder("\t\r\n");
$caret = new Clue\CaretNotation\Encoder("\t\r\n");
echo $caret->encode($logs);
},
function ($error) use ($container) {
Expand Down
4 changes: 1 addition & 3 deletions examples/pull.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
// this example shows how the imageCreateStream() call can be used to pull a given image.
// demonstrates the JSON streaming API, individual progress events will be printed as they happen.

use Clue\React\Docker\Client;

require __DIR__ . '/../vendor/autoload.php';

$image = isset($argv[1]) ? $argv[1] : 'clue/redis-benchmark';
echo 'Pulling image "' . $image . '" (pass as argument to this example)' . PHP_EOL;

$client = new Client();
$client = new Clue\React\Docker\Client();

$stream = $client->imageCreateStream($image);

Expand Down
4 changes: 1 addition & 3 deletions examples/push.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
// this example shows how the imagePush() call can be used to publish a given image.
// this requires authorization and this example includes some invalid defaults.

use Clue\React\Docker\Client;

require __DIR__ . '/../vendor/autoload.php';

$image = isset($argv[1]) ? $argv[1] : 'asd';
$auth = json_decode('{"username": "string", "password": "string", "email": "string", "serveraddress" : "string", "auth": ""}');
echo 'Pushing image "' . $image . '" (pass as argument to this example)' . PHP_EOL;

$client = new Client();
$client = new Clue\React\Docker\Client();

$client->imagePush($image, null, null, $auth)->then(function ($result) {
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
Expand Down
4 changes: 1 addition & 3 deletions examples/resize.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
// this example tries to adjust the TTY size of the given container to 10x10.
// you can check this via "docker logs".

use Clue\React\Docker\Client;

require __DIR__ . '/../vendor/autoload.php';

$container = isset($argv[1]) ? $argv[1] : 'asd';

$client = new Client();
$client = new Clue\React\Docker\Client();

$client->containerInspect($container)->then(function ($info) use ($client, $container) {
$size = $info['HostConfig']['ConsoleSize'];
Expand Down
4 changes: 1 addition & 3 deletions examples/stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
// this example shows how the `containerStatsStream()` method can be used show live container stats.
// demonstrates the JSON streaming API, individual stats events will be printed as they happen.

use Clue\React\Docker\Client;

require __DIR__ . '/../vendor/autoload.php';

$container = isset($argv[1]) ? $argv[1] : 'asd';
echo 'Monitoring "' . $container . '" (pass as argument to this example)' . PHP_EOL;

$client = new Client();
$client = new Clue\React\Docker\Client();

$stream = $client->containerStatsStream($container);

Expand Down
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function __construct(LoopInterface $loop = null, $url = null)
throw new \InvalidArgumentException('Invalid Docker Engine API URL given');
}

$browser = new Browser($loop, $connector);
$browser = new Browser($connector, $loop);
$this->browser = $browser->withBase($url);
$this->parser = new ResponseParser();
$this->streamingParser = new StreamingParser();
Expand Down
Loading