Skip to content
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
47 changes: 13 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ its event-driven model to react to changes and events happening.

* [Quickstart example](#quickstart-example)
* [Usage](#usage)
* [Factory](#factory)
* [createClient()](#createclient)
* [Client](#client)
* [Commands](#commands)
* [Promises](#promises)
Expand All @@ -48,10 +46,9 @@ Docker API of your local docker daemon:

```php
$loop = React\EventLoop\Factory::create();
$factory = new Factory($loop);
$client = $factory->createClient();
$client = new Clue\React\Docker\Client($loop);

$client->imageSearch('clue')->then(function ($images) {
$client->imageSearch('clue')->then(function (array $images) {
var_dump($images);
});

Expand All @@ -62,45 +59,28 @@ See also the [examples](examples).

## Usage

### Factory
### Client

The `Factory` is responsible for creating your `Client` instance.
It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage).
The `Client` is responsible for assembling and sending HTTP requests to the Docker Engine API.
It uses an HTTP client bound to the main [`EventLoop`](https://github.com/reactphp/event-loop#usage)
in order to handle async requests:

```php
$loop = React\EventLoop\Factory::create();
$factory = new Factory($loop);
$client = new Clue\React\Docker\Client($loop);
```

If you need custom DNS, SSL/TLS or proxy settings, you can explicitly pass a
custom [`Browser`](https://github.com/clue/php-buzz-react#browser) instance:
If your Docker Engine API is not accessible using the default `unix:///var/run/docker.sock`
Unix domain socket path, you may optionally pass an explicit URL like this:

```php
$factory = new Factory($loop, $browser);
```

#### createClient()

The `createClient($url = null)` method can be used to create a new `Client`.
It helps with constructing a `Browser` object for the given remote URL.

```php
// create client with default URL (unix:///var/run/docker.sock)
$client = $factory->createClient();

// explicitly use given UNIX socket path
$client = $factory->createClient('unix:///var/run/docker.sock');
$client = new Clue\React\Docker\Client($loop, 'unix:///var/run/docker.sock');

// connect via TCP/IP
$client = $factory->createClient('http://10.0.0.2:8000/');
// or connect via TCP/IP to a remote Docker Engine API
$client = new Clue\React\Docker\Client($loop, 'http://10.0.0.2:8000/');
```

### Client

The `Client` is responsible for assembling and sending HTTP requests to the Docker API.
It requires a `Browser` object bound to the main `EventLoop` in order to handle async requests and a base URL.
The recommended way to create a `Client` is using the `Factory` (see above).

#### Commands

All public methods on the `Client` resemble the API described in the [Docker Engine API documentation](https://docs.docker.com/develop/sdk/) like this:
Expand Down Expand Up @@ -162,8 +142,7 @@ The resulting blocking code could look something like this:
use Clue\React\Block;

$loop = React\EventLoop\Factory::create();
$factory = new Factory($loop);
$client = $factory->createClient();
$client = new Clue\React\Docker\Client($loop);

$promise = $client->imageInspect('busybox');

Expand Down
16 changes: 7 additions & 9 deletions examples/archive.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
<?php

// this example shows how the containerArchiveStream() method returns a TAR stream,
// how it can be passed to a TAR decoder and how we can then pipe each
// individual file to the console output.

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

use React\EventLoop\Factory as LoopFactory;
use Clue\React\Docker\Factory;
use Clue\CaretNotation\Encoder;
use Clue\React\Docker\Client;
use Clue\React\Tar\Decoder;
use React\Stream\ReadableStreamInterface;
use Clue\CaretNotation\Encoder;

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;

$loop = LoopFactory::create();

$factory = new Factory($loop);
$client = $factory->createClient();
$loop = React\EventLoop\Factory::create();
$client = new Client($loop);

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

Expand Down
13 changes: 5 additions & 8 deletions examples/benchmark-exec.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php

// this simple example executes a command within the given running container and
// displays how fast it can receive its output.
// expect this to be significantly faster than the (totally unfair) equivalent:
// $ docker exec asd dd if=/dev/zero bs=1M count=1000 | dd of=/dev/null

require __DIR__ . '/../vendor/autoload.php';
use Clue\React\Docker\Client;

use React\EventLoop\Factory as LoopFactory;
use Clue\React\Docker\Factory;
use React\Stream\Stream;
require __DIR__ . '/../vendor/autoload.php';

$container = 'asd';
$cmd = array('dd', 'if=/dev/zero', 'bs=1M', 'count=1000');
Expand All @@ -18,10 +17,8 @@
$cmd = array_slice($argv, 2);
}

$loop = LoopFactory::create();

$factory = new Factory($loop);
$client = $factory->createClient();
$loop = React\EventLoop\Factory::create();
$client = new Client($loop);

$client->execCreate($container, $cmd)->then(function ($info) use ($client) {
$stream = $client->execStartStream($info['Id'], true);
Expand Down
12 changes: 5 additions & 7 deletions examples/events.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
<?php

// 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.

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

use React\EventLoop\Factory as LoopFactory;
use Clue\React\Docker\Factory;
use Clue\React\Docker\Client;

$loop = LoopFactory::create();
require __DIR__ . '/../vendor/autoload.php';

$factory = new Factory($loop);
$client = $factory->createClient();
$loop = React\EventLoop\Factory::create();
$client = new Client($loop);

// 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
14 changes: 6 additions & 8 deletions examples/exec-inspect.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
// this simple example executes a "sleep 2" within the given running container

require __DIR__ . '/../vendor/autoload.php';
// this simple example executes a "sleep 2" within the given running container

use React\EventLoop\Factory as LoopFactory;
use Clue\React\Docker\Factory;
use Clue\React\Buzz\Message\ResponseException;
use Clue\React\Docker\Client;

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

$container = 'asd';
//$cmd = array('echo', 'hello world');
Expand All @@ -18,10 +18,8 @@
$cmd = array_slice($argv, 2);
}

$loop = LoopFactory::create();

$factory = new Factory($loop);
$client = $factory->createClient();
$loop = React\EventLoop\Factory::create();
$client = new Client($loop);

$client->execCreate($container, $cmd)->then(function ($info) use ($client) {
echo 'Created with info: ' . json_encode($info) . PHP_EOL;
Expand Down
14 changes: 6 additions & 8 deletions examples/exec-stream.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

// this example executes some commands within the given running container and
// displays the streaming output as it happens.

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

use React\EventLoop\Factory as LoopFactory;
use Clue\React\Docker\Factory;
use Clue\React\Docker\Client;
use React\Stream\Stream;

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

$container = 'asd';
//$cmd = array('echo', 'hello world');
//$cmd = array('sleep', '2');
Expand All @@ -19,10 +19,8 @@
$cmd = array_slice($argv, 2);
}

$loop = LoopFactory::create();

$factory = new Factory($loop);
$client = $factory->createClient();
$loop = React\EventLoop\Factory::create();
$client = new Client($loop);

$out = new Stream(STDOUT, $loop);
$out->pause();
Expand Down
14 changes: 6 additions & 8 deletions examples/export.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
<?php

// this example shows how the containerExport() call returns a TAR stream
// and how we it can be piped into a output tar file.

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

use React\EventLoop\StreamSelectLoop;
use Clue\React\Docker\Factory;
use Clue\React\Docker\Client;
use React\Stream\Stream;

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

$container = isset($argv[1]) ? $argv[1] : 'asd';
$target = isset($argv[2]) ? $argv[2] : ($container . '.tar');
echo 'Exporting whole container "' . $container . '" to "' . $target .'" (pass as arguments to this example)' . PHP_EOL;

$loop = new StreamSelectLoop();

$factory = new Factory($loop);
$client = $factory->createClient();
$loop = React\EventLoop\Factory::create();
$client = new Client($loop);

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

Expand Down
12 changes: 5 additions & 7 deletions examples/info.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<?php
// this simple example displays system wide information from Docker as a simple JSON

require __DIR__ . '/../vendor/autoload.php';
// this simple example displays system wide information from Docker as a simple JSON

use React\EventLoop\Factory as LoopFactory;
use Clue\React\Docker\Factory;
use Clue\React\Docker\Client;

$loop = LoopFactory::create();
require __DIR__ . '/../vendor/autoload.php';

$factory = new Factory($loop);
$client = $factory->createClient();
$loop = React\EventLoop\Factory::create();
$client = new Client($loop);

$client->info()->then(function ($info) {
echo json_encode($info, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
Expand Down
12 changes: 5 additions & 7 deletions examples/pull.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
<?php

// 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.

require __DIR__ . '/../vendor/autoload.php';
use Clue\React\Docker\Client;

use React\EventLoop\Factory as LoopFactory;
use Clue\React\Docker\Factory;
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;

$loop = LoopFactory::create();

$factory = new Factory($loop);
$client = $factory->createClient();
$loop = React\EventLoop\Factory::create();
$client = new Client($loop);

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

Expand Down
12 changes: 5 additions & 7 deletions examples/push.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
<?php

// 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.

require __DIR__ . '/../vendor/autoload.php';
use Clue\React\Docker\Client;

use React\EventLoop\Factory as LoopFactory;
use Clue\React\Docker\Factory;
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;

$loop = LoopFactory::create();

$factory = new Factory($loop);
$client = $factory->createClient();
$loop = React\EventLoop\Factory::create();
$client = new Client($loop);

$client->imagePush($image, null, null, $auth)->then(function ($result) {
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
Expand Down
12 changes: 5 additions & 7 deletions examples/resize.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
<?php

// this example tries to adjust the TTY size of the given container to 10x10.
// you can check this via "docker logs".

require __DIR__ . '/../vendor/autoload.php';
use Clue\React\Docker\Client;

use React\EventLoop\Factory as LoopFactory;
use Clue\React\Docker\Factory;
require __DIR__ . '/../vendor/autoload.php';

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

$loop = LoopFactory::create();

$factory = new Factory($loop);
$client = $factory->createClient();
$loop = React\EventLoop\Factory::create();
$client = new Client($loop);

$client->containerInspect($container)->then(function ($info) use ($client, $container) {
$size = $info['HostConfig']['ConsoleSize'];
Expand Down
Loading