Skip to content

Commit a7a0d7c

Browse files
committed
Accept a single command string argument for execCreate()
1 parent 20404c4 commit a7a0d7c

3 files changed

Lines changed: 58 additions & 6 deletions

File tree

src/Client.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,10 @@ public function imageSearch($term)
882882
/**
883883
* Sets up an exec instance in a running container id
884884
*
885+
* The $command should be given as an array of strings (the command plus
886+
* arguments). Alternatively, you can also pass a single command line string
887+
* which will then be wrapped in a shell process.
888+
*
885889
* The TTY mode should be set depending on whether your command needs a TTY
886890
* or not. Note that toggling the TTY mode affects how/whether you can access
887891
* the STDERR stream and also has a significant impact on performance for
@@ -900,17 +904,21 @@ public function imageSearch($term)
900904
* STDOUT/STDERR are multiplexed into separate streams + relatively slow
901905
* This looks strange to you? It probably is. Consider using the first option instead.
902906
*
903-
* @param string $container container ID
904-
* @param string $cmd Command to run specified as an array of strings
905-
* @param boolean $tty TTY mode
906-
* @param boolean $stdin attaches to STDIN of the exec command
907-
* @param boolean $stdout attaches to STDOUT of the exec command
908-
* @param boolean $stderr attaches to STDERR of the exec command
907+
* @param string $container container ID
908+
* @param string|array $cmd Command to run specified as an array of strings or a single command string
909+
* @param boolean $tty TTY mode
910+
* @param boolean $stdin attaches to STDIN of the exec command
911+
* @param boolean $stdout attaches to STDOUT of the exec command
912+
* @param boolean $stderr attaches to STDERR of the exec command
909913
* @return PromiseInterface Promise<array> with exec ID in the form of `array("Id" => $execId)`
910914
* @link https://docs.docker.com/reference/api/docker_remote_api_v1.15/#exec-create
911915
*/
912916
public function execCreate($container, $cmd, $tty = false, $stdin = false, $stdout = true, $stderr = true)
913917
{
918+
if (!is_array($cmd)) {
919+
$cmd = array('sh', '-c', (string)$cmd);
920+
}
921+
914922
return $this->postJson(
915923
$this->uri->expand(
916924
'/containers/{container}/exec',

tests/ClientTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,14 @@ public function testExecCreate()
384384
$this->expectPromiseResolveWith($json, $this->client->execCreate(123, array('env')));
385385
}
386386

387+
public function testExecCreateStringCommand()
388+
{
389+
$json = array();
390+
$this->expectRequestFlow('post', '/containers/123/exec', $this->createResponseJson($json), 'expectJson');
391+
392+
$this->expectPromiseResolveWith($json, $this->client->execCreate(123, 'env'));
393+
}
394+
387395
public function testExecDetached()
388396
{
389397
$body = '';

tests/FunctionalClientTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,42 @@ public function testExecInspectAfterRunning($exec)
153153
$this->assertEquals(0, $info['ExitCode']);
154154
}
155155

156+
/**
157+
* @depends testStartRunning
158+
* @param string $container
159+
*/
160+
public function testExecStringCommandWithOutputWhileRunning($container)
161+
{
162+
$promise = $this->client->execCreate($container, 'echo -n hello world');
163+
$exec = Block\await($promise, $this->loop);
164+
165+
$this->assertTrue(is_array($exec));
166+
$this->assertTrue(is_string($exec['Id']));
167+
168+
$promise = $this->client->execStart($exec['Id'], true);
169+
$output = Block\await($promise, $this->loop);
170+
171+
$this->assertEquals('hello world', $output);
172+
}
173+
174+
/**
175+
* @depends testStartRunning
176+
* @param string $container
177+
*/
178+
public function testExecStringCommandWithStderrOutputWhileRunning($container)
179+
{
180+
$promise = $this->client->execCreate($container, 'echo -n hello world >&2', true);
181+
$exec = Block\await($promise, $this->loop);
182+
183+
$this->assertTrue(is_array($exec));
184+
$this->assertTrue(is_string($exec['Id']));
185+
186+
$promise = $this->client->execStart($exec['Id'], true);
187+
$output = Block\await($promise, $this->loop);
188+
189+
$this->assertEquals('hello world', $output);
190+
}
191+
156192
/**
157193
* @depends testStartRunning
158194
* @param string $container

0 commit comments

Comments
 (0)