From 2a6df3d431e16a0b153cec1df8247b1031b3e72a Mon Sep 17 00:00:00 2001 From: Dibyajyoti Panda Date: Mon, 18 Jun 2018 02:42:49 +0530 Subject: [PATCH 1/2] Update container-run.md Added an example for executing command on running container. --- docs/cookbook/container-run.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docs/cookbook/container-run.md b/docs/cookbook/container-run.md index 0820e80a..6876d79e 100644 --- a/docs/cookbook/container-run.md +++ b/docs/cookbook/container-run.md @@ -208,3 +208,29 @@ $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 +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); +$output = $docker->execStart($execid,$execStartConfig); + +var_dump($output); +``` From bbc493426d853581bc4aa73dc95b323fb05606d7 Mon Sep 17 00:00:00 2001 From: Dibyajyoti Panda Date: Mon, 18 Jun 2018 03:14:54 +0530 Subject: [PATCH 2/2] Update container-run.md --- docs/cookbook/container-run.md | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/docs/cookbook/container-run.md b/docs/cookbook/container-run.md index 6876d79e..2ee824bf 100644 --- a/docs/cookbook/container-run.md +++ b/docs/cookbook/container-run.md @@ -230,7 +230,22 @@ $execConfig->setCmd(['mkdir', '/tmp/testDir']); $execid = $docker->containerExec('android',$execConfig)->getId(); $execStartConfig = new ExecIdStartPostBody(); $execStartConfig->setDetach(false); -$output = $docker->execStart($execid,$execStartConfig); -var_dump($output); +// 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 ]) ; ```