Skip to content

Commit 9cd1ad4

Browse files
committed
Add containerCreate() and containerStart() methods
1 parent 725bca9 commit 9cd1ad4

3 files changed

Lines changed: 85 additions & 0 deletions

File tree

src/Client.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ public function containerList($all = false, $size = false)
8888
return $this->browser->get($this->url('/containers/json?all=%u&size=%u', $all, $size))->then(array($this->parser, 'expectJson'));
8989
}
9090

91+
/**
92+
* Create a container
93+
*
94+
* @param array $config e.g. `array('Image' => 'busybox', 'Cmd' => 'date')` (see link)
95+
* @param string|null $name (optional) name to assign to this container
96+
* @return Promise Promise<array> container properties `array('Id' => $containerId', 'Warnings' => array())`
97+
* @link https://docs.docker.com/reference/api/docker_remote_api_v1.15/#create-a-container
98+
*/
99+
public function containerCreate($config, $name = null)
100+
{
101+
return $this->postJson($this->url('/containers/create?name=%s', $name), $config)->then(array($this->parser, 'expectJson'));
102+
}
103+
91104
/**
92105
* Return low-level information on the container id
93106
*
@@ -126,6 +139,19 @@ public function containerResize($container, $w, $h)
126139
return $this->browser->get($this->url('/containers/%s/resize?w=%u&h=%u', $container, $w, $h))->then(array($this->parser, 'expectEmpty'));
127140
}
128141

142+
/**
143+
* Start the container id
144+
*
145+
* @param string $container container ID
146+
* @param array $config (optional) start config (see link)
147+
* @return Promise Promise<null>
148+
* @link https://docs.docker.com/reference/api/docker_remote_api_v1.15/#start-a-container
149+
*/
150+
public function containerStart($container, $config = array())
151+
{
152+
return $this->postJson($this->url('/containers/%s/start', $container), $config)->then(array($this->parser, 'expectEmpty'));
153+
}
154+
129155
/**
130156
* Stop the container id
131157
*

tests/ClientTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ public function testPing()
2626
$this->expectPromiseResolveWith($body, $this->client->ping());
2727
}
2828

29+
public function testContainerCreate()
30+
{
31+
$json = array();
32+
$config = array();
33+
$this->expectRequestFlow('post', '/containers/create?name=', $this->createResponseJson($json), 'expectJson');
34+
35+
$this->expectPromiseResolveWith($json, $this->client->containerCreate($config));
36+
}
37+
38+
public function testContainerCreateName()
39+
{
40+
$json = array();
41+
$config = array();
42+
$this->expectRequestFlow('post', '/containers/create?name=demo', $this->createResponseJson($json), 'expectJson');
43+
44+
$this->expectPromiseResolveWith($json, $this->client->containerCreate($config, 'demo'));
45+
}
46+
2947
public function testContainerInspect()
3048
{
3149
$json = array();
@@ -71,6 +89,14 @@ public function testContainerKillSignalNumber()
7189
$this->expectPromiseResolveWith('', $this->client->containerKill(123, 9));
7290
}
7391

92+
public function testContainerStart()
93+
{
94+
$config = array();
95+
$this->expectRequestFlow('post', '/containers/123/start', $this->createResponse(), 'expectEmpty');
96+
97+
$this->expectPromiseResolveWith('', $this->client->containerStart(123, $config));
98+
}
99+
74100
public function testContainerStop()
75101
{
76102
$this->expectRequestFlow('post', '/containers/123/stop?t=10', $this->createResponse(), 'expectEmpty');

tests/FunctionalClientTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,39 @@ public function testPing()
3232
$this->loop->run();
3333
}
3434

35+
public function testCreateStartAndRemoveContainer()
36+
{
37+
$config = array(
38+
'Image' => 'busybox',
39+
'Cmd' => array('echo', 'test')
40+
);
41+
42+
$promise = $this->client->containerCreate($config);
43+
$container = $this->waitFor($promise, $this->loop);
44+
45+
$this->assertNotNull($container['Id']);
46+
$this->assertNull($container['Warnings']);
47+
48+
$promise = $this->client->containerStart($container['Id']);
49+
$ret = $this->waitFor($promise, $this->loop);
50+
51+
$this->assertEquals('', $ret);
52+
53+
$promise = $this->client->containerRemove($container['Id'], false, true);
54+
$ret = $this->waitFor($promise, $this->loop);
55+
56+
$this->assertEquals('', $ret);
57+
}
58+
59+
/**
60+
* @expectedException RuntimeException
61+
*/
62+
public function testContainerRemoveInvalid()
63+
{
64+
$promise = $this->client->containerRemove('invalid123');
65+
$this->waitFor($promise, $this->loop);
66+
}
67+
3568
public function testInfo()
3669
{
3770
$this->expectPromiseResolve($this->client->info());

0 commit comments

Comments
 (0)