forked from docker-php/docker-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocker.php
More file actions
157 lines (132 loc) · 4.28 KB
/
Copy pathDocker.php
File metadata and controls
157 lines (132 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace Docker;
use Docker\Context\Context;
use Docker\Http\DockerClient;
use Docker\Http\Stream\StreamCallbackInterface;
use Docker\Manager\ContainerManager;
use Docker\Manager\ImageManager;
use Docker\Exception\UnexpectedStatusCodeException;
use Docker\Context\ContextInterface;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Stream\Stream;
/**
* Docker\Docker
*/
class Docker
{
const BUILD_VERBOSE = false;
const BUILD_QUIET = true;
const BUILD_CACHE = true;
const BUILD_NO_CACHE = false;
/**
* @var \GuzzleHttp\Client
*/
private $httpClient;
/**
* @var array
*/
private $containerManager;
/**
* @var array
*/
private $imageManager;
/**
* @param HttpClient $httpClient Http client to use with Docker
*/
public function __construct(HttpClient $httpClient = null)
{
$this->httpClient = $httpClient ?: new DockerClient();
}
/**
* @return \GuzzleHttp\Client
*/
public function getHttpClient()
{
return $this->httpClient;
}
/**
* @return \Docker\Manager\ContainerManager
*/
public function getContainerManager()
{
if (null === $this->containerManager) {
$this->containerManager = new ContainerManager($this->httpClient);
}
return $this->containerManager;
}
/**
* @return \Docker\Manager\ImageManager
*/
public function getImageManager()
{
if (null === $this->imageManager) {
$this->imageManager = new ImageManager($this->httpClient);
}
return $this->imageManager;
}
/**
* Build an image with docker
*
* @param \Docker\Context\ContextInterface $context Context to build
* @param string $name Name of the wanted image
* @param callable $callback A callback to be called for having log of build
* @param boolean $quiet Quiet build (doest not output commands during build)
* @param boolean $cache Use docker cache
* @param boolean $rm Remove intermediate container during build
*/
public function build(ContextInterface $context, $name, callable $callback = null, $quiet = false, $cache = true, $rm = false)
{
$content = is_resource($context->read()) ? new Stream($context->read()) : $context->read();
$response = $this->httpClient->post(['/build{?data*}', ['data' => [
'q' => (integer) $quiet,
't' => $name,
'nocache' => (integer) !$cache,
'rm' => (integer) $rm
]]], [
'headers' => array('Content-Type' => 'application/tar'),
'body' => $content,
'stream' => true
]);
if (null === $callback) {
$callback = function() {};
}
$stream = $response->getBody();
if ($stream instanceof StreamCallbackInterface) {
$stream->readWithCallback($callback);
} else {
$callback($stream->__toString(), null);
}
}
/**
* Commit a container into an image
*
* @param \Docker\Container $container
* @param array $config
*
* @throws Exception\UnexpectedStatusCodeException
*
* @return \Docker\Image
*
* @see http://docs.docker.io/en/latest/api/docker_remote_api_v1.7/#create-a-new-image-from-a-container-s-changes
*/
public function commit(Container $container, $config = array())
{
if (isset($config['run'])) {
$config['run'] = json_encode($config['run']);
}
$config['container'] = $container->getId();
$response = $this->httpClient->post(['/commit{?config*}', ['config' => $config]]);
if ($response->getStatusCode() !== "201") {
throw new UnexpectedStatusCodeException($response->getStatusCode(), (string) $response->getBody());
}
$image = new Image();
$image->setId($response->json()['Id']);
if (array_key_exists('repo', $config)) {
$image->setRepository($config['repo']);
}
if (array_key_exists('tag', $config)) {
$image->setTag($config['tag']);
}
return $image;
}
}