Skip to content

Commit d55d009

Browse files
authored
Merge pull request #74 from dinooo13/commit
Add commit API endpoint
2 parents 11f5d3a + 054d39e commit d55d009

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

src/Client.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,39 @@ public function imageSearch($term)
11861186
)->then(array($this->parser, 'expectJson'));
11871187
}
11881188

1189+
/**
1190+
* Create a new image from a container
1191+
*
1192+
* @param ?string $container The ID or name of the container to commit
1193+
* @param ?string $repo Repository name for the created image
1194+
* @param ?string $tag Tag name for the created image
1195+
* @param ?string $comment Commit message
1196+
* @param ?string $author Author of the image (e.g., John Hannibal Smith <[email protected]>)
1197+
* @param bool $pause Whether to pause the container before committing, default: true
1198+
* @param ?string $changes Dockerfile's instructions to apply while committing
1199+
* @param array $config The container configuration (body of the request)
1200+
* @return PromiseInterface
1201+
* @link https://docs.docker.com/engine/api/v1.41/#operation/ImageCommit
1202+
*/
1203+
public function containerCommit($container, $repo = null, $tag = null, $comment = null, $author = null, $pause = true, $changes = null, array $config = array())
1204+
{
1205+
return $this->postJson(
1206+
$this->uri->expand(
1207+
'commit{?container,repo,tag,comment,author,pause,changes}',
1208+
array(
1209+
'container' => $container,
1210+
'repo' => $repo,
1211+
'tag' => $tag,
1212+
'comment' => $comment,
1213+
'author' => $author,
1214+
'pause' => $pause,
1215+
'changes' => $changes,
1216+
)
1217+
),
1218+
$config
1219+
)->then(array($this->parser, 'expectJson'));
1220+
}
1221+
11891222
/**
11901223
* Sets up an exec instance in a running container id
11911224
*

tests/ClientTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,24 @@ public function testImageSearch()
601601
$this->expectPromiseResolveWith($json, $this->client->imageSearch('clue'));
602602
}
603603

604+
public function testCommit()
605+
{
606+
$json = array();
607+
$config = array();
608+
$this->expectRequestFlow('post', 'commit?container=123&pause=1', $this->createResponseJson($json), 'expectJson');
609+
610+
$this->expectPromiseResolveWith($json, $this->client->containerCommit('123', $config));
611+
}
612+
613+
public function testCommitWithAllParams()
614+
{
615+
$json = array();
616+
$config = array();
617+
$this->expectRequestFlow('post', 'commit?container=123&repo=docker.io&tag=latest&comment=hello&author=batman&pause=1&changes=EXPOSE%208080', $this->createResponseJson($json), 'expectJson');
618+
619+
$this->expectPromiseResolveWith($json, $this->client->containerCommit('123', 'docker.io', 'latest', 'hello', 'batman', true, 'EXPOSE 8080', $config));
620+
}
621+
604622
public function testExecCreate()
605623
{
606624
$json = array();

tests/FunctionalClientTest.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ public function testCreateConnectDisconnectAndRemoveNetwork()
435435
'Image' => 'busybox',
436436
'Cmd' => array('echo', 'test')
437437
);
438-
$networkName = uniqid('reactphp-docker');
438+
$networkName = uniqid('reactphp-docker', true);
439439

440440
$promise = $this->client->containerCreate($containerConfig);
441441
$container = Block\await($promise, Loop::get());
@@ -473,9 +473,53 @@ public function testCreateConnectDisconnectAndRemoveNetwork()
473473
$ret = Block\await($promise, Loop::get());
474474

475475
// expects "create", "disconnect", "destroy" events ("connect" will be skipped because we don't start the container)
476-
$this->assertEquals(3, count($ret));
476+
$this->assertCount(3, $ret);
477477
$this->assertEquals('create', $ret[0]['Action']);
478478
$this->assertEquals('disconnect', $ret[1]['Action']);
479479
$this->assertEquals('destroy', $ret[2]['Action']);
480480
}
481+
482+
/**
483+
* @depends testImageInspectCheckIfBusyboxExists
484+
*/
485+
public function testCreateAndCommitContainer()
486+
{
487+
$config = array(
488+
'Image' => 'busybox',
489+
'Cmd' => array('echo', 'test')
490+
);
491+
492+
$promise = $this->client->containerCreate($config);
493+
$container = Block\await($promise, Loop::get());
494+
495+
$this->assertNotNull($container['Id']);
496+
$this->assertEmpty($container['Warnings']);
497+
498+
$promise = $this->client->containerCommit($container['Id']);
499+
$image = Block\await($promise, Loop::get());
500+
501+
$this->assertNotNull($image['Id']);
502+
$this->assertArrayNotHasKey('message', $image);
503+
504+
$promise = $this->client->containerRemove($container['Id'], false, true);
505+
$ret = Block\await($promise, Loop::get());
506+
507+
$this->assertEquals('', $ret);
508+
509+
$config = array(
510+
'Image' => $image['Id'],
511+
'Cmd' => array('echo', 'test')
512+
);
513+
514+
$promise = $this->client->containerCreate($config);
515+
$container = Block\await($promise, Loop::get());
516+
517+
$this->assertNotNull($container['Id']);
518+
$this->assertEmpty($container['Warnings']);
519+
520+
$promise = $this->client->containerRemove($container['Id'], false, true);
521+
$ret = Block\await($promise, Loop::get());
522+
523+
$this->assertEquals('', $ret);
524+
}
481525
}

0 commit comments

Comments
 (0)