Skip to content
This repository was archived by the owner on Oct 26, 2019. It is now read-only.
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
13 changes: 13 additions & 0 deletions docs/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,16 @@ $response = $manager->execstart($execid, function ($log, $type) use($logger) {
//Response stream is never read you need to simulate a wait in order to get output
$response->getBody()->getContents();
```

### Inspect a process

You can get various details about an exec instance and its current state:

```
$execid = $manager->exec($container, ["/bin/bash", "-c", "ls /var/www/html"]);
$inspection = $manager->execinspect($execid);

echo $inspection->Running;
echo $inspection->ExitCode;
// there's a lot more
```
22 changes: 22 additions & 0 deletions src/Docker/Manager/ContainerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,28 @@ public function execstart($execid, callable $callback = null, $detach = false, $
return $response;
}

/**
* Get details about an executive
*
* @param string $execid Identifier from exec()
*
* @throws \Docker\Exception\UnexpectedStatusCodeException
*
* @return stdClass

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's inconsistent with the other methods that returns a ResponseInterface object btw 👀

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of exec() you might need/want to interact with the output as a stream or whatever, which is not the case here, or did I miss something?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some methods return ResponseInterface. There's plenty of strings/arrays being returned too.

*/
public function execinspect($execid)
{
$response = $this->client->get(['/exec/{id}/json', [
'id' => $execid
]]);

if ($response->getStatusCode() !== "200") {
throw UnexpectedStatusCodeException::fromResponse($response);
}

return json_decode((string) $response->getBody());
}

/**
* Attach a container to a callback to read logs
*
Expand Down
21 changes: 21 additions & 0 deletions src/Docker/Tests/Manager/ContainerManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,27 @@ public function testExec()
$this->assertEquals('output', $output);
}

public function testExecInspect()
{
$manager = $this->getManager();
$dockerFileBuilder = new ContextBuilder();
$dockerFileBuilder->from('ubuntu:precise');
$dockerFileBuilder->add('/daemon.sh', file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'script' . DIRECTORY_SEPARATOR . 'daemon.sh'));
$dockerFileBuilder->run('chmod +x /daemon.sh');

$this->getDocker()->build($dockerFileBuilder->getContext(), 'docker-php-restart-test', null, true, false, true);

$container = new Container(['Image' => 'docker-php-restart-test', 'Cmd' => ['/daemon.sh']]);
$manager->create($container);
$manager->start($container);

$execId = $manager->exec($container, ['/bin/bash', '-c', 'echo -n "output"']);
$inspection = $manager->execinspect($execId);

$this->assertEquals(0, $inspection->ExitCode);
$this->assertEquals(false, $inspection->Running);
}

public function testRename()
{
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/true']]);
Expand Down