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

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

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

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

$loop = LoopFactory::create();

$factory = new Factory($loop);
$client = $factory->createClient();

$client->execCreate($container, array('Cmd' => array('sleep', '2'), 'AttachStdout' => true))->then(function ($info) use ($client) {
echo 'Created with info: ' . json_encode($info) . PHP_EOL;

return $client->execInspect($info['Id']);
})->then(function ($info) use ($client) {
echo 'Inspected after creation: ' . json_encode($info, JSON_PRETTY_PRINT) . PHP_EOL;

return $client->execStart($info['ID'], array())->then(function ($out) use ($client, $info) {
echo 'Starting returned: ';
var_dump($out);

return $client->execInspect($info['ID']);
});
})->then(function ($info) {
echo 'Inspected after execution: ' . json_encode($info, JSON_PRETTY_PRINT) . PHP_EOL;
}, function (Exception $e) {
echo 'ERROR: ' . $e->getMessage() . PHP_EOL;

if ($e instanceof ResponseException) {
echo 'Response: ' . $e->getResponse()->getBody() . PHP_EOL;
}
});

$loop->run();
21 changes: 21 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,27 @@ public function execResize($exec, $w, $h)
)->then(array($this->parser, 'expectEmpty'));
}

/**
* Returns low-level information about the exec command id.
*
* Requires API v1.16+ / Docker v1.4+
*
* @param string $exec exec ID
* @return PromiseInterface Promise<array>
* @link https://docs.docker.com/engine/reference/api/docker_remote_api_v1.16/#exec-inspect
*/
public function execInspect($exec)
{
return $this->browser->get(
$this->uri->expand(
'/exec/{exec}/json',
array(
'exec' => $exec
)
)
)->then(array($this->parser, 'expectJson'));
}

private function postJson($url, $data)
{
$body = $this->json($data);
Expand Down
8 changes: 8 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,14 @@ public function testExecResize()
$this->expectPromiseResolveWith('', $this->client->execResize(123, 800, 600));
}

public function testExecInspect()
{
$json = array();
$this->expectRequestFlow('get', '/exec/123/json', $this->createResponseJson($json), 'expectJson');

$this->expectPromiseResolveWith($json, $this->client->execInspect(123));
}

private function expectRequestFlow($method, $url, ResponseInterface $response, $parser)
{
$return = (string)$response->getBody();
Expand Down