Skip to content
This repository was archived by the owner on Oct 26, 2019. It is now read-only.
Merged
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
41 changes: 41 additions & 0 deletions docs/cookbook/container-run.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,44 @@ $hostConfig->setPortBindings($portMap);

$containerConfig->setHostConfig($hostConfig);
```

## Executing a command on a running container

This example shows how you can execute an command on any running container. It is same as `docker exec CONTAINER_ID some_command `.

```php
<?php
use Docker\Docker;
use Docker\API\Model\ContainersIdExecPostBody;
use Docker\API\Model\ExecIdStartPostBody;

$docker = Docker::create();

$execConfig = new ContainersIdExecPostBody();
$execConfig->setTty(true);
$execConfig->setAttachStdout(true);
$execConfig->setAttachStderr(true);
$execConfig->setCmd(['mkdir', '/tmp/testDir']);

$execid = $docker->containerExec('android',$execConfig)->getId();
$execStartConfig = new ExecIdStartPostBody();
$execStartConfig->setDetach(false);

// Execute the command
$stream = $docker->execStart($execid,$execStartConfig);

// To see the output stream of the 'exec' command
$stdoutText = "";
$stderrText = "";

$stream->onStdout(function ($stdout) use (&$stdoutText) {
$stdoutText .= $stdout;
});

$stream->onStderr(function ($stderr) use (&$stderrText) {
$stderrText .= $stderr;
});

$stream->wait();
var_dump([ "stdout" => $stdoutText, "stderr" => $stderrText ]) ;
```
Comment thread
dbjpanda marked this conversation as resolved.