-
Notifications
You must be signed in to change notification settings - Fork 0
/
Docker.php
562 lines (489 loc) · 11.8 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
<?php
namespace dophpsdk;
/**
* dophpsdk\Docker
* Several docker management commands and utilities.
*/
class Docker {
/**
* @ToDo: Make this abstract to allow usage for more than one containers
*
* Start one or more stopped containers.
* See "docker start --help" for more details.
*
* @param string $unique
* @return bool
*/
public function ContainerStart($unique) {
$cmd = "docker start " . $unique;
exec($cmd, $result);
return $result == $unique;
}
/**
* @ToDo: Make this abstract to allow usage for more than one containers.
*
* Restart one or more containers.
* See "docker restart --help" for more details.
*
* @param string $unique
* @return bool
*/
public function ContainerRestart($unique) {
$cmd = "docker restart " . $unique;
exec($cmd, $result);
return $result == $unique;
}
/**
* @ToDo: Make this abstract to allow usage for more than one containers.
*
* Stop one or more containers.
* See "docker stop --help" for more details.
*
* @param string $unique
* @return bool
*/
public function ContainerStop($unique) {
$cmd = "docker stop " . $unique;
exec($cmd, $result);
return $result == $unique;
}
/**
* Check if container is running.
* If running returns "true" else "false".
* If there is no such container returns "null".
*
* @param string $unique
* @return boolean|null
*/
public function ContainerIsRunning($unique) {
$cmd = "docker inspect --format='{{.State.Running}}' ";
$cmd .= $unique;
$cmd .= " 2>&1";
exec($cmd, $result);
if (isset($result[1])) {
return NULL;
}
if ($result[0] == "false") {
return FALSE;
} else {
return TRUE;
}
}
/**
* Create a new container.
* See "docker create --help" for more details.
*
* Returns the new container id or FALSE.
*
* @param string $options
* @param string $name
* @param string $image
*
* @return string|bool
*/
public function ContainerCreate($image, $name = "", $options = "") {
$cmd = "docker create ";
if ($name) {
$cmd .= " --name " . $name;
}
$cmd .= " " . $options;
$cmd .= " " . $image;
$cmd .= " 2>&1";
exec($cmd, $result);
if (isset($result[0]) && !isset($result[1])) {
// If success returns the container ID
return $result;
} else {
return FALSE;
}
}
/**
* Run a command in a new container.
* See "docker run --help" for more details.
* Notice that there is no need to add the "/bin/sh -c" before the actual command.
*
* @param string $image
* @param string $name
* @param string $command
* @param string $options
*
* @return string|bool
*/
public function ContainerRun($image, $name = "", $command = "", $options = "") {
$cmd = "docker run -d ";
if ($name) {
$cmd .= "--name " . $name;
}
$cmd .= " " . $options;
$cmd .= " " . $image;
if ($command) {
$cmd .= " /bin/sh -c " . $command;
}
exec($cmd, $result);
if (isset($result[0]) && !isset($result[1])) {
// If success returns the container ID
return $result;
} else {
return FALSE;
}
}
/**
* Remove a container.
* See "docker rm --help" for more details.
* Notice that if the container is running it will not be removed except if using "-f" as options.
*
* @param string $unique
* @param string $options
*
* @return bool
*/
public function ContainerRm($unique, $options = "") {
$cmd = "docker rm ";
$cmd .= $options;
$cmd .= " " . $unique;
$cmd .= " 2>&1";
exec($cmd, $result);
if ($result[0] == $unique) {
// If success returns TRUE
return TRUE;
} else {
return FALSE;
}
}
/**
* Kill a container.
* See "docker kill --help" for more details.
*
* @param string $unique
* @param string $options
*
* @return bool
*/
public function ContainerKill($unique, $options = "") {
$cmd = "docker kill ";
$cmd .= $options;
$cmd .= " " . $unique;
$cmd .= " 2>&1";
exec($cmd, $result);
if ($result[0] == $unique) {
// If success returns TRUE
return TRUE;
} else {
return FALSE;
}
}
/**
* Delete a container for ever. Use with caution.
*
* @param $unique
*
* @return bool
*/
public function ContainerDelete($unique) {
return $this->ContainerRm($unique, "-f");
}
/**
* Rename a container (change name).
* See "docker rename --help" for more details.
*
* @param string $name
* @param string $unique
*
* @return bool
*/
public function ContainerRename($unique, $name) {
$cmd = "docker rename ";
$cmd .= $unique;
$cmd .= " " . $this::sanitizeContainerName($name);
exec($cmd, $result);
if (!isset($result[0])) {
// If success returns TRUE
return TRUE;
} else {
return FALSE;
}
}
/**
* Inspect a container using Id or Name.
* See "docker inspect --help" for more details.
*
* Returns an array from the json data.
* We can use var_damp etc to get the array keys/values.
*
* @param string $options
* @param string $unique
*
* @return mixed
*/
public function ContainerInspect($unique, $options = "") {
$cmd = "docker inspect ";
$cmd .= $options;
$cmd .= "--format '{{json .}}' ";
$cmd .= " " . $unique;
exec($cmd, $result);
return $this::JsonOutputToArray($result, "Id");
}
/**
* Fetch the logs of a container.
* See "docker logs --help" for more details.
*
* @param string $options
* @param string $unique
*
* @return array
*/
public function ContainerLogs($unique, $options = "") {
$cmd = "docker logs ";
$cmd .= $options . " ";
$cmd .= $unique;
$cmd .= " 2>&1";
exec($cmd, $result);
return $result;
}
/**
* Get container stats in array.
* See "docker stats --help" for more details.
*
* @param string $unique
* @return mixed
*/
public function ContainerStats($unique = "") {
$cmd = "docker stats ";
$cmd .= " --no-stream --no-trunc --format '{{json .}}' ";
$cmd .= $unique;
exec($cmd, $result);
return $this->JsonOutputToArray($result, "ID");
}
/**
* Run a command in a running container.
* See "docker exec --help" for more details.
*
* @param string $unique
* @param string $command
*
* @return array|bool
*/
public function ContainerExec($unique, $command) {
$cmd = "docker exec -ti ";
$cmd .= " ". $unique;
$cmd .= " ". $command;
$cmd .= " 2>&1";
exec($cmd, $result);
if ($result[0] == "Error: No such container: " . $unique) {
return FALSE;
} else {
return $result;
}
}
/**
* Update configuration of one or more containers.
* See "docker update --help" for more details.
*
* @param string $unique
* @param string $options
*
* @return bool
*/
public function ContainerUpdate($unique, $options) {
$cmd = "docker update ";
$cmd .= $options;
$cmd .= " " . $unique;
$cmd .= " 2>&1";
exec($cmd, $result);
if ($result[0] == $unique) {
return TRUE;
} else {
return $result;
}
}
/**
* Pause all processes within a container.
* See "docker pause --help" for more details.
*
* @param string $unique
*
* @return array|bool
*/
public function ContainerPause($unique) {
$cmd = "docker pause " . $unique;
$cmd .= " 2>&1";
exec($cmd, $result);
if (isset($result[0]) && $result[0] == $unique) {
return TRUE;
} else {
return $result;
}
}
/**
* Unpause all processes within a container.
* See "docker unpause --help" for more details.
*
* @param string $unique
*
* @return array|bool
*/
public function ContainerUnpause($unique) {
$cmd = "docker unpause " . $unique;
$cmd .= " 2>&1";
exec($cmd, $result);
if (isset($result[0]) && $result[0] == $unique) {
return TRUE;
} else {
return $result;
}
}
/**
* Commit a container to create a new image.
* See "docker commit --help" for more details.
*
* @param string $image | The new docker image in the format [image]:[tag]
* @param string $unique
* @param string $options
*
* @return array|bool
*/
public function ContainerCommit($unique, $image, $options = "") {
$cmd = "docker commit ";
$cmd .= $options;
$cmd .= " " . $unique;
$cmd .= " " . $image;
$cmd .= " 2>&1";
exec($cmd, $result);
if (isset($result[0]) && $result[0] == $unique) {
return TRUE;
} else {
return $result;
}
}
/**
* List containers.
* See "docker ps --help" for more details.
*
* @param string $options
* @return array
*/
public function ps($options = "-a") {
$cmd = "docker ps ";
$cmd .= $options;
$cmd .= " --format '{{json .}}'";
exec($cmd, $result);
return $this->JsonOutputToArray($result, "ID");
}
/**
* List Images.
* See "docker images --help" for more details.
*
* @param string $options
* @param string $image
* @return array|bool
*/
public function images($options = "", $image = "") {
$cmd = "docker images ";
$cmd .= $options;
$cmd .= " --no-trunc --digests --format '{{json .}}' ";
$cmd .= $image;
exec($cmd, $result);
return $this::JsonOutputToArray($result, "ID");
}
/**
* Remove one or more images.
* See "docker rmi --help" for more details.
*
* @param string $image
* @param string $options
*
* @return array|bool
*/
public function rmi($image, $options = "") {
$cmd = "docker rmi ";
$cmd .= $options;
$cmd .= " " . $image;
$cmd .= " 2>&1";
exec($cmd, $result);
if (strpos($result[0], 'Error') === 0) {
return $result;
} else {
return TRUE;
}
}
/**
* Inspect changes to files or directories on a container's filesystem.
* See "docker diff --help" for more details.
*
* @param $unique
*
* @return array|bool
*/
public function ContainerDiff($unique) {
$cmd = "docker diff ";
$cmd .= $unique;
$cmd .= " 2>&1";
exec($cmd, $result);
return $result;
}
/**
* Docker info
*
* @return array
*/
public function info() {
$cmd = "docker info --format '{{json .}}' ";
exec($cmd, $result);
return json_decode($result[0], TRUE);
}
/**
* Docker version
*
* @return array
*/
public function version() {
$cmd = "docker version --format '{{json .}}' ";
exec($cmd, $result);
return json_decode($result[0], TRUE);
}
/**
* Generate an array from a json output
*
* @param array $result
* @param string $id
*
* @return array|bool
*/
static function JsonOutputToArray($result, $id) {
if ($result) {
$array = [];
foreach ($result as $key => $value) {
// Decode json values
$value = json_decode($value, TRUE);
// Create the new key
if ($value[$id]) {
$k = $value[$id];
// Add new data to array
$array[$k] = $value;
}
}
if (!empty($array)) {
return $array;
} else {
return FALSE;
}
} else {
return FALSE;
}
}
/**
* Sanitize strings to create the container names (helper function).
*
* @param $string
* @return string
*/
static function sanitizeContainerName($string) {
//$string = strtolower($string);
$regex = "/[^a-zA-Z0-9\-\_\.]/";
$string = preg_replace($regex, '', $string);
$string = trim(preg_replace('/[\s\t\n\r\s]+/', ' ', $string));
//$string = preg_replace('/__/', '_', $string);
$string = substr($string, 0, 40);
return $string;
}
}