-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Client.php
1650 lines (1571 loc) · 61.9 KB
/
Client.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Clue\React\Docker;
use Clue\React\Docker\Io\ResponseParser;
use Clue\React\Docker\Io\StreamingParser;
use React\EventLoop\LoopInterface;
use React\Http\Browser;
use React\Promise\PromiseInterface;
use React\Socket\FixedUriConnector;
use React\Socket\UnixConnector;
use React\Stream\ReadableStreamInterface;
use Rize\UriTemplate;
/**
* The Docker Engine API client can be used to control your (local) Docker daemon.
*
* This Client implementation provides a very thin wrapper around this
* Docker Engine API and exposes its exact data models.
* The Client uses HTTP requests via a local UNIX socket path or remotely via a
* TLS-backed TCP/IP connection.
*
* This client has been tested against a variety of Docker Engine API versions,
* with support for Docker Engine API v1.40 (and newer) and versions as old as
* Docker Engine API v1.14. See also https://docs.docker.com/engine/api/version-history/.
*
* @link https://docs.docker.com/develop/sdk/
*/
class Client
{
private $browser;
private $parser;
private $streamingParser;
private $uri;
/**
*
* This class takes an optional `LoopInterface|null $loop` parameter that can be used to
* pass the event loop instance to use for this object. You can use a `null` value
* here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
* This value SHOULD NOT be given unless you're sure you want to explicitly use a
* given event loop instance.
*
* If your Docker Engine API is not accessible using the default `unix:///var/run/docker.sock`
* Unix domain socket path, you may optionally pass an explicit URL like this:
*
* ```php
* // explicitly use given UNIX socket path
* $client = new Clue\React\Docker\Client(null, 'unix:///var/run/docker.sock');
*
* // or connect via TCP/IP to a remote Docker Engine API
* $client = new Clue\React\Docker\Client(null, 'http://10.0.0.2:8000/');
* ```
*
* @param ?LoopInterface $loop
* @param ?string $url
* @throws \InvalidArgumentException
*/
public function __construct($loop = null, $url = null)
{
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
throw new \InvalidArgumentException('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
}
if ($url === null) {
$url = 'unix:///var/run/docker.sock';
}
$connector = null;
if (substr($url, 0, 7) === 'unix://') {
// send everything through a local unix domain socket
$connector = new FixedUriConnector(
$url,
new UnixConnector($loop)
);
// pretend all HTTP URLs to be on localhost
$url = 'http://localhost/';
}
$parts = parse_url($url);
if (!isset($parts['scheme'], $parts['host']) || ($parts['scheme'] !== 'http' && $parts['scheme'] !== 'https')) {
throw new \InvalidArgumentException('Invalid Docker Engine API URL given');
}
$browser = new Browser($connector, $loop);
$this->browser = $browser->withBase($url);
$this->parser = new ResponseParser();
$this->streamingParser = new StreamingParser();
$this->uri = new UriTemplate();
}
/**
* Ping the docker server
*
* @return PromiseInterface Promise<string> "OK"
* @link https://docs.docker.com/engine/api/v1.40/#operation/SystemPing
*/
public function ping()
{
return $this->browser->get('_ping')->then(array($this->parser, 'expectPlain'));
}
/**
* Display system-wide information
*
* @return PromiseInterface Promise<array> system info (see link)
* @link https://docs.docker.com/engine/api/v1.40/#operation/SystemInfo
*/
public function info()
{
return $this->browser->get('info')->then(array($this->parser, 'expectJson'));
}
/**
* Show the docker version information
*
* @return PromiseInterface Promise<array> version info (see link)
* @link https://docs.docker.com/engine/api/v1.40/#operation/SystemVersion
*/
public function version()
{
return $this->browser->get('version')->then(array($this->parser, 'expectJson'));
}
/**
* Get container events from docker
*
* This is a JSON streaming API endpoint that resolves with an array of all
* individual progress events.
*
* If you need to access the individual progress events as they happen, you
* should consider using `eventsStream()` instead.
*
* Note that this method will buffer all events until the stream closes.
* This means that you SHOULD pass a timestamp for `$until` so that this
* method only polls the given time interval and then resolves.
*
* The optional `$filters` parameter can be used to only get events for
* certain event types, images and/or containers etc. like this:
* <code>
* $filters = array(
* 'image' => array('ubuntu', 'busybox'),
* 'event' => array('create')
* );
* </code>
*
* @param float|null $since timestamp used for polling
* @param float|null $until timestamp used for polling
* @param array $filters (optional) filters to apply (requires Docker Engine API v1.16+ / Docker v1.4+)
* @return PromiseInterface Promise<array> array of event objects
* @link https://docs.docker.com/engine/api/v1.40/#operation/SystemEvents
* @uses self::eventsStream()
* @see self::eventsStream()
*/
public function events($since = null, $until = null, $filters = array())
{
return $this->streamingParser->deferredStream(
$this->eventsStream($since, $until, $filters)
);
}
/**
* Get container events from docker
*
* This is a JSON streaming API endpoint that returns a stream instance.
*
* The resulting stream will emit the following events:
* - data: for *each* element in the update stream
* - error: once if an error occurs, will close() stream then
* - close: once the stream ends (either finished or after "error")
*
* The optional `$filters` parameter can be used to only get events for
* certain event types, images and/or containers etc. like this:
* <code>
* $filters = array(
* 'image' => array('ubuntu', 'busybox'),
* 'event' => array('create')
* );
* </code>
*
* @param float|null $since timestamp used for polling
* @param float|null $until timestamp used for polling
* @param array $filters (optional) filters to apply (requires Docker Engine API v1.16+ / Docker v1.4+)
* @return ReadableStreamInterface stream of event objects
* @link https://docs.docker.com/engine/api/v1.40/#operation/SystemEvents
* @see self::events()
*/
public function eventsStream($since = null, $until = null, $filters = array())
{
return $this->streamingParser->parseJsonStream(
$this->browser->requestStreaming(
'GET',
$this->uri->expand(
'events{?since,until,filters}',
array(
'since' => $since,
'until' => $until,
'filters' => $filters ? json_encode($filters) : null
)
)
)
);
}
/**
* List containers
*
* @param boolean $all
* @param boolean $size
* @return PromiseInterface Promise<array> list of container objects
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerList
*/
public function containerList($all = false, $size = false)
{
return $this->browser->get(
$this->uri->expand(
'containers/json{?all,size}',
array(
'all' => $this->boolArg($all),
'size' => $this->boolArg($size)
)
)
)->then(array($this->parser, 'expectJson'));
}
/**
* Create a container
*
* @param array $config e.g. `array('Image' => 'busybox', 'Cmd' => 'date')` (see link)
* @param string|null $name (optional) name to assign to this container
* @return PromiseInterface Promise<array> container properties `array('Id' => $containerId', 'Warnings' => array())`
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerCreate
*/
public function containerCreate($config, $name = null)
{
return $this->postJson(
$this->uri->expand(
'containers/create{?name}',
array(
'name' => $name
)
),
$config
)->then(array($this->parser, 'expectJson'));
}
/**
* Return low-level information on the container id
*
* @param string $container container ID
* @return PromiseInterface Promise<array> container properties
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerInspect
*/
public function containerInspect($container)
{
return $this->browser->get(
$this->uri->expand(
'containers/{container}/json',
array(
'container' => $container
)
)
)->then(array($this->parser, 'expectJson'));
}
/**
* List processes running inside the container id
*
* @param string $container container ID
* @param string|null $ps_args (optional) ps arguments to use (e.g. aux)
* @return PromiseInterface Promise<array>
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerTop
*/
public function containerTop($container, $ps_args = null)
{
return $this->browser->get(
$this->uri->expand(
'containers/{container}/top{?ps_args}',
array(
'container' => $container,
'ps_args' => $ps_args
)
)
)->then(array($this->parser, 'expectJson'));
}
/**
* Get stdout and stderr logs from the container id
*
* This resolves with a string containing the log output, i.e. STDOUT
* and STDERR as requested.
*
* Keep in mind that this means the whole string has to be kept in memory.
* For bigger container logs it's usually a better idea to use a streaming
* approach, see containerLogsStream() for more details.
* In particular, the same also applies for the $follow flag. It can be used
* to follow the container log messages as long as the container is running.
*
* Note that this endpoint works only for containers with the "json-file" or
* "journald" logging drivers.
*
* Note that this endpoint internally has to check the `containerInspect()`
* endpoint first in order to figure out the TTY settings to properly decode
* the raw log output.
*
* @param string $container container ID
* @param boolean $follow 1/True/true or 0/False/false, return stream. Default false
* @param boolean $stdout 1/True/true or 0/False/false, show stdout log. Default true
* @param boolean $stderr 1/True/true or 0/False/false, show stderr log. Default true
* @param int $since UNIX timestamp (integer) to filter logs. Specifying a timestamp will only output log-entries since that timestamp. Default: 0 (unfiltered) (requires API v1.19+ / Docker v1.7+)
* @param boolean $timestamps 1/True/true or 0/False/false, print timestamps for every log line. Default false
* @param int|null $tail Output specified number of lines at the end of logs: all or <number>. Default all
* @return PromiseInterface Promise<string> log output string
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerLogs
* @uses self::containerLogsStream()
* @see self::containerLogsStream()
*/
public function containerLogs($container, $follow = false, $stdout = true, $stderr = true, $since = 0, $timestamps = false, $tail = null)
{
return $this->streamingParser->bufferedStream(
$this->containerLogsStream($container, $follow, $stdout, $stderr, $since, $timestamps, $tail)
);
}
/**
* Get stdout and stderr logs from the container id
*
* This is a streaming API endpoint that returns a readable stream instance
* containing the the log output, i.e. STDOUT and STDERR as requested.
*
* This works for container logs of arbitrary sizes as only small chunks have to
* be kept in memory.
*
* This is particularly useful for the $follow flag. It can be used
* to follow the container log messages as long as the container is running.
*
* Note that by default the output of both STDOUT and STDERR will be emitted
* as normal "data" events. You can optionally pass a custom event name which
* will be used to emit STDERR data so that it can be handled separately.
* Note that the normal streaming primitives likely do not know about this
* event, so special care may have to be taken.
* Also note that this option has no effect if the container has been
* created with a TTY.
*
* Note that this endpoint works only for containers with the "json-file" or
* "journald" logging drivers.
*
* Note that this endpoint internally has to check the `containerInspect()`
* endpoint first in order to figure out the TTY settings to properly decode
* the raw log output.
*
* @param string $container container ID
* @param boolean $follow 1/True/true or 0/False/false, return stream. Default false
* @param boolean $stdout 1/True/true or 0/False/false, show stdout log. Default true
* @param boolean $stderr 1/True/true or 0/False/false, show stderr log. Default true
* @param int $since UNIX timestamp (integer) to filter logs. Specifying a timestamp will only output log-entries since that timestamp. Default: 0 (unfiltered) (requires API v1.19+ / Docker v1.7+)
* @param boolean $timestamps 1/True/true or 0/False/false, print timestamps for every log line. Default false
* @param int|null $tail Output specified number of lines at the end of logs: all or <number>. Default all
* @param string $stderrEvent custom event to emit for STDERR data (otherwise emits as "data")
* @return ReadableStreamInterface log output stream
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerLogs
* @see self::containerLogs()
*/
public function containerLogsStream($container, $follow = false, $stdout = true, $stderr = true, $since = 0, $timestamps = false, $tail = null, $stderrEvent = null)
{
$parser = $this->streamingParser;
$browser = $this->browser;
$url = $this->uri->expand(
'containers/{container}/logs{?follow,stdout,stderr,since,timestamps,tail}',
array(
'container' => $container,
'follow' => $this->boolArg($follow),
'stdout' => $this->boolArg($stdout),
'stderr' => $this->boolArg($stderr),
'since' => ($since === 0) ? null : $since,
'timestamps' => $this->boolArg($timestamps),
'tail' => $tail
)
);
// first inspect container to check TTY setting, then request logs with appropriate log parser
return \React\Promise\Stream\unwrapReadable($this->containerInspect($container)->then(function ($info) use ($url, $browser, $parser, $stderrEvent) {
$stream = $parser->parsePlainStream($browser->requestStreaming('GET', $url));
if (!$info['Config']['Tty']) {
$stream = $parser->demultiplexStream($stream, $stderrEvent);
}
return $stream;
}));
}
/**
* Inspect changes on container id's filesystem
*
* @param string $container container ID
* @return PromiseInterface Promise<array>
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerChanges
*/
public function containerChanges($container)
{
return $this->browser->get(
$this->uri->expand(
'containers/{container}/changes',
array(
'container' => $container
)
)
)->then(array($this->parser, 'expectJson'));
}
/**
* Export the contents of container id
*
* This resolves with a string in the TAR file format containing all files
* in the container.
*
* Keep in mind that this means the whole string has to be kept in memory.
* For bigger containers it's usually a better idea to use a streaming
* approach, see containerExportStream() for more details.
*
* Accessing individual files in the TAR file format string is out of scope
* for this library. Several libraries are available, one that is known to
* work is clue/reactphp-tar (see links).
*
* @param string $container container ID
* @return PromiseInterface Promise<string> tar stream
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerExport
* @link https://github.com/clue/reactphp-tar
* @see self::containerExportStream()
*/
public function containerExport($container)
{
return $this->browser->get(
$this->uri->expand(
'containers/{container}/export',
array(
'container' => $container
)
)
)->then(array($this->parser, 'expectPlain'));
}
/**
* Export the contents of container id
*
* This returns a stream in the TAR file format containing all files
* in the container.
*
* This works for containers of arbitrary sizes as only small chunks have to
* be kept in memory.
*
* Accessing individual files in the TAR file format stream is out of scope
* for this library. Several libraries are available, one that is known to
* work is clue/reactphp-tar (see links).
*
* The resulting stream is a well-behaving readable stream that will emit
* the normal stream events.
*
* @param string $container container ID
* @return ReadableStreamInterface tar stream
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerExport
* @link https://github.com/clue/reactphp-tar
* @see self::containerExport()
*/
public function containerExportStream($container)
{
return $this->streamingParser->parsePlainStream(
$this->browser->requestStreaming(
'GET',
$this->uri->expand(
'containers/{container}/export',
array(
'container' => $container
)
)
)
);
}
/**
* Returns a container’s resource usage statistics.
*
* This is a JSON API endpoint that resolves with a single stats info.
*
* If you want to monitor live stats events as they happen, you
* should consider using `imageStatsStream()` instead.
*
* Available as of Docker Engine API v1.19 (Docker v1.7), use `containerStatsStream()` on legacy versions
*
* @param string $container container ID
* @return PromiseInterface Promise<array> JSON stats
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerStats
* @see self::containerStatsStream()
*/
public function containerStats($container)
{
return $this->browser->get(
$this->uri->expand(
'containers/{container}/stats{?stream}',
array(
'container' => $container,
'stream' => 0
)
)
)->then(array($this->parser, 'expectJson'));
}
/**
* Returns a live stream of a container’s resource usage statistics.
*
* The resulting stream will emit the following events:
* - data: for *each* element in the stats stream
* - error: once if an error occurs, will close() stream then
* - close: once the stream ends (either finished or after "error")
*
* Available as of Docker Engine API v1.17 (Docker v1.5)
*
* @param string $container container ID
* @return ReadableStreamInterface JSON stats stream
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerStats
* @see self::containerStats()
*/
public function containerStatsStream($container)
{
return $this->streamingParser->parseJsonStream(
$this->browser->requestStreaming(
'GET',
$this->uri->expand(
'containers/{container}/stats',
array(
'container' => $container
)
)
)
);
}
/**
* Resize the TTY of container id
*
* @param string $container container ID
* @param int $w TTY width
* @param int $h TTY height
* @return PromiseInterface Promise<null>
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerResize
*/
public function containerResize($container, $w, $h)
{
return $this->browser->post(
$this->uri->expand(
'containers/{container}/resize{?w,h}',
array(
'container' => $container,
'w' => $w,
'h' => $h,
)
)
)->then(array($this->parser, 'expectEmpty'));
}
/**
* Start the container id
*
* @param string $container container ID
* @return PromiseInterface Promise<null>
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerStart
*/
public function containerStart($container)
{
return $this->browser->post(
$this->uri->expand(
'containers/{container}/start',
array(
'container' => $container
)
)
)->then(array($this->parser, 'expectEmpty'));
}
/**
* Stop the container id
*
* @param string $container container ID
* @param null|int $t (optional) number of seconds to wait before killing the container
* @return PromiseInterface Promise<null>
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerStop
*/
public function containerStop($container, $t = null)
{
return $this->browser->post(
$this->uri->expand(
'containers/{container}/stop{?t}',
array(
'container' => $container,
't' => $t
)
)
)->then(array($this->parser, 'expectEmpty'));
}
/**
* Restart the container id
*
* @param string $container container ID
* @param null|int $t (optional) number of seconds to wait before killing the container
* @return PromiseInterface Promise<null>
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerRestart
*/
public function containerRestart($container, $t = null)
{
return $this->browser->post(
$this->uri->expand(
'containers/{container}/restart{?t}',
array(
'container' => $container,
't' => $t
)
)
)->then(array($this->parser, 'expectEmpty'));
}
/**
* Kill the container id
*
* @param string $container container ID
* @param string|int|null $signal (optional) signal name or number
* @return PromiseInterface Promise<null>
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerKill
*/
public function containerKill($container, $signal = null)
{
return $this->browser->post(
$this->uri->expand(
'containers/{container}/kill{?signal}',
array(
'container' => $container,
'signal' => $signal
)
)
)->then(array($this->parser, 'expectEmpty'));
}
/**
* Rename the container id
*
* Requires Docker Engine API v1.17+ / Docker v1.5+
*
* @param string $container container ID
* @param string $name new name for the container
* @return PromiseInterface Promise<null>
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerRename
*/
public function containerRename($container, $name)
{
return $this->browser->post(
$this->uri->expand(
'containers/{container}/rename{?name}',
array(
'container' => $container,
'name' => $name
)
)
)->then(array($this->parser, 'expectEmpty'));
}
/**
* Pause the container id
*
* @param string $container container ID
* @return PromiseInterface Promise<null>
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerPause
*/
public function containerPause($container)
{
return $this->browser->post(
$this->uri->expand(
'containers/{container}/pause',
array(
'container' => $container
)
)
)->then(array($this->parser, 'expectEmpty'));
}
/**
* Unpause the container id
*
* @param string $container container ID
* @return PromiseInterface Promise<null>
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerUnpause
*/
public function containerUnpause($container)
{
return $this->browser->post(
$this->uri->expand(
'containers/{container}/unpause',
array(
'container' => $container
)
)
)->then(array($this->parser, 'expectEmpty'));
}
/**
* Attach to a container to read its output.
*
* This resolves with a string containing the container output, i.e. STDOUT
* and STDERR as requested.
*
* Keep in mind that this means the whole string has to be kept in memory.
* For a larger container output it's usually a better idea to use a streaming
* approach, see `containerAttachStream()` for more details.
* In particular, the same also applies for the `$stream` flag. It can be used
* to follow the container output as long as the container is running.
*
* Note that this endpoint internally has to check the `containerInspect()`
* endpoint first in order to figure out the TTY settings to properly decode
* the raw container output.
*
* @param string $container container ID
* @param bool $logs replay previous logs before attaching. Default false
* @param bool $stream continue streaming. Default false
* @param bool $stdout attach to stdout. Default true
* @param bool $stderr attach to stderr. Default true
* @return PromiseInterface Promise<string> container output string
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerAttach
* @uses self::containerAttachStream()
* @see self::containerAttachStream()
*/
public function containerAttach($container, $logs = false, $stream = false, $stdout = true, $stderr = true)
{
return $this->streamingParser->bufferedStream(
$this->containerAttachStream($container, $logs, $stream, $stdout, $stderr)
);
}
/**
* Attach to a container to read its output.
*
* This is a streaming API endpoint that returns a readable stream instance
* containing the container output, i.e. STDOUT and STDERR as requested.
*
* This works for container output of arbitrary sizes as only small chunks have to
* be kept in memory.
*
* This is particularly useful for the `$stream` flag. It can be used to
* follow the container output as long as the container is running. Either
* the `$stream` or `$logs` parameter must be `true` for this endpoint to do
* anything meaningful.
*
* Note that by default the output of both STDOUT and STDERR will be emitted
* as normal "data" events. You can optionally pass a custom event name which
* will be used to emit STDERR data so that it can be handled separately.
* Note that the normal streaming primitives likely do not know about this
* event, so special care may have to be taken.
* Also note that this option has no effect if the container has been
* created with a TTY.
*
* Note that this endpoint internally has to check the `containerInspect()`
* endpoint first in order to figure out the TTY settings to properly decode
* the raw container output.
*
* Note that this endpoint intentionally does not expose the `$stdin` flag.
* Access to STDIN will be exposed as a dedicated API endpoint in a future
* version.
*
* @param string $container container ID
* @param bool $logs replay previous logs before attaching. Default false
* @param bool $stream continue streaming. Default false
* @param bool $stdout attach to stdout. Default true
* @param bool $stderr attach to stderr. Default true
* @param string $stderrEvent custom event to emit for STDERR data (otherwise emits as "data")
* @return ReadableStreamInterface container output stream
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerAttach
* @see self::containerAttach()
*/
public function containerAttachStream($container, $logs = false, $stream = false, $stdout = true, $stderr = true, $stderrEvent = null)
{
$parser = $this->streamingParser;
$browser = $this->browser;
$url = $this->uri->expand(
'containers/{container}/attach{?logs,stream,stdout,stderr}',
array(
'container' => $container,
'logs' => $this->boolArg($logs),
'stream' => $this->boolArg($stream),
'stdout' => $this->boolArg($stdout),
'stderr' => $this->boolArg($stderr)
)
);
// first inspect container to check TTY setting, then attach with appropriate log parser
return \React\Promise\Stream\unwrapReadable($this->containerInspect($container)->then(function ($info) use ($url, $browser, $parser, $stderrEvent) {
$stream = $parser->parsePlainStream($browser->requestStreaming('POST', $url));
if (!$info['Config']['Tty']) {
$stream = $parser->demultiplexStream($stream, $stderrEvent);
}
return $stream;
}));
}
/**
* Block until container id stops, then returns the exit code
*
* @param string $container container ID
* @return PromiseInterface Promise<array> `array('StatusCode' => 0)` (see link)
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerWait
*/
public function containerWait($container)
{
return $this->browser->post(
$this->uri->expand(
'containers/{container}/wait',
array(
'container' => $container
)
)
)->then(array($this->parser, 'expectJson'));
}
/**
* Remove the container id from the filesystem
*
* @param string $container container ID
* @param boolean $v Remove the volumes associated to the container. Default false
* @param boolean $force Kill then remove the container. Default false
* @return PromiseInterface Promise<null>
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerRemove
*/
public function containerRemove($container, $v = false, $force = false)
{
return $this->browser->delete(
$this->uri->expand(
'containers/{container}{?v,force}',
array(
'container' => $container,
'v' => $this->boolArg($v),
'force' => $this->boolArg($force)
)
)
)->then(array($this->parser, 'expectEmpty'));
}
/**
* Get a tar archive of a resource in the filesystem of container id.
*
* This resolves with a string in the TAR file format containing all files
* specified in the given $path.
*
* Keep in mind that this means the whole string has to be kept in memory.
* For bigger containers it's usually a better idea to use a streaming approach,
* see containerArchiveStream() for more details.
*
* Accessing individual files in the TAR file format string is out of scope
* for this library. Several libraries are available, one that is known to
* work is clue/reactphp-tar (see links).
*
* Available as of Docker Engine API v1.20 (Docker v1.8)
*
* @param string $container container ID
* @param string $resource path to file or directory to archive
* @return PromiseInterface Promise<string> tar stream
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerArchive
* @link https://github.com/clue/reactphp-tar
* @see self::containerArchiveStream()
*/
public function containerArchive($container, $path)
{
return $this->browser->get(
$this->uri->expand(
'containers/{container}/archive{?path}',
array(
'container' => $container,
'path' => $path
)
)
)->then(array($this->parser, 'expectPlain'));
}
/**
* Get a tar archive of a resource in the filesystem of container id.
*
* This returns a stream in the TAR file format containing all files
* specified in the given $path.
*
* This works for (any number of) files of arbitrary sizes as only small chunks have to
* be kept in memory.
*
* Accessing individual files in the TAR file format stream is out of scope
* for this library. Several libraries are available, one that is known to
* work is clue/reactphp-tar (see links).
*
* The resulting stream is a well-behaving readable stream that will emit
* the normal stream events.
*
* Available as of Docker Engine API v1.20 (Docker v1.8)
*
* @param string $container container ID
* @param string $path path to file or directory to archive
* @return ReadableStreamInterface tar stream
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerArchive
* @link https://github.com/clue/reactphp-tar
* @see self::containerArchive()
*/
public function containerArchiveStream($container, $path)
{
return $this->streamingParser->parsePlainStream(
$this->browser->requestStreaming(
'GET',
$this->uri->expand(
'containers/{container}/archive{?path}',
array(
'container' => $container,
'path' => $path
)
)
)
);
}
/**
* List images
*
* @param boolean $all
* @return PromiseInterface Promise<array> list of image objects
* @link https://docs.docker.com/engine/api/v1.40/#operation/ImageList
* @todo support $filters param
*/
public function imageList($all = false)
{
return $this->browser->get(
$this->uri->expand(
'images/json{?all}',
array(
'all' => $this->boolArg($all)
)
)
)->then(array($this->parser, 'expectJson'));
}
/**
* Create an image, either by pulling it from the registry or by importing it
*
* This is a JSON streaming API endpoint that resolves with an array of all
* individual progress events.
*
* If you want to access the individual progress events as they happen, you
* should consider using `imageCreateStream()` instead.
*
* Pulling a private image from a remote registry will likely require authorization, so make
* sure to pass the $registryAuth parameter, see `self::authHeaders()` for
* more details.
*
* @param string|null $fromImage name of the image to pull
* @param string|null $fromSrc source to import, - means stdin
* @param string|null $repo repository
* @param string|null $tag (optional) (obsolete) tag, use $repo and $fromImage in the "name:tag" instead
* @param string|null $registry the registry to pull from
* @param array|null $registryAuth AuthConfig object (to send as X-Registry-Auth header)
* @return PromiseInterface Promise<array> stream of message objects
* @link https://docs.docker.com/engine/api/v1.40/#operation/ImageCreate
* @uses self::imageCreateStream()
*/
public function imageCreate($fromImage = null, $fromSrc = null, $repo = null, $tag = null, $registry = null, $registryAuth = null)
{
return $this->streamingParser->deferredStream(
$this->imageCreateStream($fromImage, $fromSrc, $repo, $tag, $registry, $registryAuth)
);
}
/**
* Create an image, either by pulling it from the registry or by importing it
*
* This is a JSON streaming API endpoint that returns a stream instance.
*
* The resulting stream will emit the following events:
* - data: for *each* element in the update stream
* - error: once if an error occurs, will close() stream then
* - close: once the stream ends (either finished or after "error").
*
* Pulling a private image from a remote registry will likely require authorization, so make
* sure to pass the $registryAuth parameter, see `self::authHeaders()` for
* more details.
*
* @param string|null $fromImage name of the image to pull
* @param string|null $fromSrc source to import, - means stdin
* @param string|null $repo repository
* @param string|null $tag (optional) (obsolete) tag, use $repo and $fromImage in the "name:tag" instead
* @param string|null $registry the registry to pull from
* @param array|null $registryAuth AuthConfig object (to send as X-Registry-Auth header)
* @return ReadableStreamInterface
* @link https://docs.docker.com/engine/api/v1.40/#operation/ImageCreate
* @see self::imageCreate()
* @uses self::authHeaders()
*/
public function imageCreateStream($fromImage = null, $fromSrc = null, $repo = null, $tag = null, $registry = null, $registryAuth = null)