-
Notifications
You must be signed in to change notification settings - Fork 0
/
Docker.php
269 lines (244 loc) · 6.71 KB
/
Docker.php
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
namespace SoftBricks\Docker;
class Docker
{
/**
* Checks if given output array contains given string
*
* @param $output
* @param $string
* @return bool
*/
private function outputContains($output, $string)
{
foreach($output as $line) {
if (strpos($line, $string) !== false) {
return true;
}
}
return false;
}
/**
* Executes docker command with given args
*
* @param array|string $args
* @return array
*/
public function executeCommand($args = [])
{
// we needs the "args" argument to be an array
if (!is_array($args)) {
$args = [ $args ];
}
$outputBuffer = [];
exec('docker ' . implode(' ', $args), $outputBuffer);
return $outputBuffer;
}
/**
* Returns list of available docker container
*
* @param bool $all
* @return Container[]
*/
public function ps($all = false)
{
$fieldMap = [
'id' => '{{.ID}}',
'image' => '{{.Image}}',
'command' => '{{.Command}}',
'createdAt' => '{{.CreatedAt}}',
'runningFor' => '{{.RunningFor}}',
'ports' => '{{.Ports}}',
'status' => '{{.Status}}',
'size' => '{{.Size}}',
'names' => '{{.Names}}',
'labels' => '{{.Labels}}',
'mounts' => '{{.Mounts}}',
'networks' => '{{.Networks}}',
];
// run docker ps to receive list of docker container.
// we do this
$cellGlue = '##---##';
$format = "table " . implode($cellGlue, $fieldMap);
$containerRaw = $this->executeCommand([
'ps',
($all ? ' --all' : ''),
'--format "' . $format . '"',
]);
// first line ouf output is just table header
$containerList = [];
if (count($containerRaw) > 1) {
$containerFields = array_keys($fieldMap);
for ($index = 1; $index < count($containerRaw); $index++) {
$line = $containerRaw[$index];
$parsedOutputLine = explode($cellGlue, $line);
// fill container object using analysed output line
$container = new Container();
foreach($parsedOutputLine as $key => $value) {
$container->{$containerFields[$key]} = $value;
}
// we return this generated container object later
$containerList[] = $container;
}
}
return $containerList;
}
/**
* Checks if there is a container running under the given name
*
* @param $name
* @return bool
*/
public function isContainerRunning($name)
{
foreach($this->ps() as $container) {
if (in_array($name, $container->getNames())) {
return true;
}
}
return false;
}
/**
* Checks if there is a container existing under the given name
*
* @param $name
* @return bool
*/
public function isContainerExisting($name)
{
foreach($this->ps(true) as $container) {
if (in_array($name, $container->getNames())) {
return true;
}
}
return false;
}
/**
* Returns container object for given container name
*
* @param $name
* @return null|Container
*/
public function getContainerInfo($name)
{
foreach($this->ps(true) as $container) {
if (in_array($name, $container->getNames())) {
return $container;
}
}
return null;
}
/**
* Starts container with given name. Return if container could be started or not.
* Will return true if container is already running.
*
* @param $name
* @return bool
*/
public function start($name)
{
if (!$this->isContainerRunning($name)) {
$output = $this->executeCommand([
'start',
$name
]);
return $output[0] === $name;
}
return true;
}
/**
* Stops container with given name. Returns if container could be stopped or not.
* Will return true if container is not running.
*
* @param $name
* @return bool
*/
public function stop($name)
{
if ($this->isContainerRunning($name)) {
$output = $this->executeCommand([
'stop',
$name
]);
return $output[0] === $name;
}
return true;
}
/**
* Kills existing docker container. Returns if container could be killed.
* Will return true if docker is not running.
*
* @param $name
* @return bool
*/
public function kill($name)
{
if ($this->isContainerRunning($name)) {
$this->executeCommand([
'kill',
$name
]);
return $this->isContainerRunning($name);
}
return true;
}
/**
* Removes container with given name. Returns if container could be removed.
* Will return true if container is not existing.
*
* @param $name
* @param bool $killIfRunning
* @return bool
*/
public function remove($name, $killIfRunning = true)
{
if ($this->isContainerExisting($name)) {
// running container can not be removed
if ($this->isContainerRunning($name) && !$killIfRunning) {
return false;
}
$this->kill($name);
$this->executeCommand([
'rm',
$name,
]);
return !$this->isContainerExisting($name);
}
return true;
}
/**
* Runs container under given name with given arguments. Returns if container could be run.
* Returns false if container is already running.
*
* @param $name
* @param array $args
* @return bool
*/
public function run($name, $args = [])
{
if (!$this->isContainerExisting($name)) {
$this->executeCommand(array_merge(
[
'run',
'--name '.$name,
],
$args
));
return $this->isContainerRunning($name);
}
return false;
}
/**
* Checks if docker is installed correctly or not
* (needs to be available over PATH)
*
* @return bool
*/
public function isInstalled()
{
return $this->outputContains(
$this->executeCommand('--version'),
'Docker version'
);
}
}