Skip to content

Commit

Permalink
Added commit API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dinooo13 committed Feb 19, 2022
1 parent 11f5d3a commit 054d39e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
33 changes: 33 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,39 @@ public function imageSearch($term)
)->then(array($this->parser, 'expectJson'));
}

/**
* Create a new image from a container
*
* @param ?string $container The ID or name of the container to commit
* @param ?string $repo Repository name for the created image
* @param ?string $tag Tag name for the created image
* @param ?string $comment Commit message
* @param ?string $author Author of the image (e.g., John Hannibal Smith <[email protected]>)
* @param bool $pause Whether to pause the container before committing, default: true
* @param ?string $changes Dockerfile's instructions to apply while committing
* @param array $config The container configuration (body of the request)
* @return PromiseInterface
* @link https://docs.docker.com/engine/api/v1.41/#operation/ImageCommit
*/
public function containerCommit($container, $repo = null, $tag = null, $comment = null, $author = null, $pause = true, $changes = null, array $config = array())
{
return $this->postJson(
$this->uri->expand(
'commit{?container,repo,tag,comment,author,pause,changes}',
array(
'container' => $container,
'repo' => $repo,
'tag' => $tag,
'comment' => $comment,
'author' => $author,
'pause' => $pause,
'changes' => $changes,
)
),
$config
)->then(array($this->parser, 'expectJson'));
}

/**
* Sets up an exec instance in a running container id
*
Expand Down
18 changes: 18 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,24 @@ public function testImageSearch()
$this->expectPromiseResolveWith($json, $this->client->imageSearch('clue'));
}

public function testCommit()
{
$json = array();
$config = array();
$this->expectRequestFlow('post', 'commit?container=123&pause=1', $this->createResponseJson($json), 'expectJson');

$this->expectPromiseResolveWith($json, $this->client->containerCommit('123', $config));
}

public function testCommitWithAllParams()
{
$json = array();
$config = array();
$this->expectRequestFlow('post', 'commit?container=123&repo=docker.io&tag=latest&comment=hello&author=batman&pause=1&changes=EXPOSE%208080', $this->createResponseJson($json), 'expectJson');

$this->expectPromiseResolveWith($json, $this->client->containerCommit('123', 'docker.io', 'latest', 'hello', 'batman', true, 'EXPOSE 8080', $config));
}

public function testExecCreate()
{
$json = array();
Expand Down
48 changes: 46 additions & 2 deletions tests/FunctionalClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ public function testCreateConnectDisconnectAndRemoveNetwork()
'Image' => 'busybox',
'Cmd' => array('echo', 'test')
);
$networkName = uniqid('reactphp-docker');
$networkName = uniqid('reactphp-docker', true);

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

// expects "create", "disconnect", "destroy" events ("connect" will be skipped because we don't start the container)
$this->assertEquals(3, count($ret));
$this->assertCount(3, $ret);
$this->assertEquals('create', $ret[0]['Action']);
$this->assertEquals('disconnect', $ret[1]['Action']);
$this->assertEquals('destroy', $ret[2]['Action']);
}

/**
* @depends testImageInspectCheckIfBusyboxExists
*/
public function testCreateAndCommitContainer()
{
$config = array(
'Image' => 'busybox',
'Cmd' => array('echo', 'test')
);

$promise = $this->client->containerCreate($config);
$container = Block\await($promise, Loop::get());

$this->assertNotNull($container['Id']);
$this->assertEmpty($container['Warnings']);

$promise = $this->client->containerCommit($container['Id']);
$image = Block\await($promise, Loop::get());

$this->assertNotNull($image['Id']);
$this->assertArrayNotHasKey('message', $image);

$promise = $this->client->containerRemove($container['Id'], false, true);
$ret = Block\await($promise, Loop::get());

$this->assertEquals('', $ret);

$config = array(
'Image' => $image['Id'],
'Cmd' => array('echo', 'test')
);

$promise = $this->client->containerCreate($config);
$container = Block\await($promise, Loop::get());

$this->assertNotNull($container['Id']);
$this->assertEmpty($container['Warnings']);

$promise = $this->client->containerRemove($container['Id'], false, true);
$ret = Block\await($promise, Loop::get());

$this->assertEquals('', $ret);
}
}

0 comments on commit 054d39e

Please sign in to comment.