UNPKG

27.1 kBPlain TextView Raw
1'use strict'
2
3import Modem = require('docker-modem')
4import { Image } from './image'
5import fs = require('fs')
6
7/**
8 * Class representing container execution
9 */
10
11export class Exec {
12 modem: Modem
13 container: Container
14 id: String
15 data: Object = {}
16
17 /**
18 * Create an execution
19 * @param {Modem} modem Modem to connect to the remote service
20 * @param {Container} container Container that owns the execution (optional)
21 * @param {string} id Id of the execution
22 */
23
24 constructor (modem: Modem, container: Container, id: String) {
25 this.modem = modem
26 this.container = container
27 this.id = id
28 }
29
30 /**
31 * Create an exec instance in a container
32 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/create-a-container
33 * @param {Object} opts Query params in the request (optional)
34 * @return {Promise} Promise return the new exec instance
35 */
36 create (opts?: Object): Promise<Exec> {
37 const call = {
38 path: `/containers/${this.container.id}/exec?`,
39 method: 'POST',
40 options: opts,
41 statusCodes: {
42 200: true,
43 201: true,
44 404: 'no such container',
45 409: 'container is paused',
46 500: 'server error'
47 }
48 }
49
50 return new Promise((resolve, reject) => {
51 this.modem.dial(call, (err, conf) => {
52 if (err) return reject(err)
53 const exec = new Exec(this.modem, this.container, conf.Id)
54 exec.data = conf
55 resolve(exec)
56 })
57 })
58 }
59
60 /**
61 * Start an exec instance
62 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/exec-start
63 * @param {Object} opts Query params in the request (optional)
64 * @return {Promise} Promise return the stream to the execution
65 */
66 start (opts: any = {}): Promise<Object> {
67 const call = {
68 path: `/exec/${this.id}/start?`,
69 method: 'POST',
70 options: opts,
71 isStream: true,
72 hijack: opts.hijack,
73 openStdin: opts.stdin,
74 statusCodes: {
75 200: true,
76 404: 'no such exec instance',
77 409: 'container is paused'
78 }
79 }
80
81 return new Promise((resolve, reject) => {
82 this.modem.dial(call, (err, stream: Object) => {
83 if (err) return reject(err)
84 resolve(stream)
85 })
86 })
87 }
88
89 /**
90 * Resize an exec instance
91 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/exec-resize
92 * @param {Object} opts Query params in the request (optional)
93 * @return {Promise} Promise return the result
94 */
95 resize (opts?: Object): Promise<{}> {
96 const call = {
97 path: `/exec/${this.id}/resize?`,
98 method: 'POST',
99 options: opts,
100 statusCodes: {
101 201: true,
102 404: 'no such exec instance'
103 }
104 }
105
106 return new Promise((resolve, reject) => {
107 this.modem.dial(call, (err) => {
108 if (err) return reject(err)
109 resolve()
110 })
111 })
112 }
113
114 /**
115 * Get status of an exec instance
116 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/exec-inspect
117 * The reason why this module isn't called inspect is because that interferes with the inspect utility of node.
118 * @param {Object} opts Query params in the request (optional)
119 * @return {Promise} Promise return the exec instance
120 */
121 status (opts?: Object): Promise<Exec> {
122 const call = {
123 path: `/exec/${this.id}/json?`,
124 method: 'GET',
125 options: opts,
126 statusCodes: {
127 200: true,
128 404: 'no such exec instance',
129 500: 'server error'
130 }
131 }
132
133 return new Promise((resolve, reject) => {
134 this.modem.dial(call, (err, conf) => {
135 if (err) return reject(err)
136 const exec = new Exec(this.modem, this.container, conf.Id)
137 exec.data = conf
138 resolve(exec)
139 })
140 })
141 }
142}
143
144/**
145 * Class representing container execution management
146 */
147export class ExecManager {
148 modem: Modem
149 container: Container
150
151 /**
152 * Create an execution
153 * @param {Modem} modem Modem to connect to the remote service
154 * @param {Container} container Container that owns the execution (optional)
155 * @param {string} id Id of the execution
156 */
157
158 constructor (modem: Modem, container: Container) {
159 this.modem = modem
160 this.container = container
161 }
162
163 /**
164 * Get a Exec object
165 * @param {id} string ID of the exec
166 * @return {Exec}
167 */
168 get (id: String): Exec {
169 return new Exec(this.modem, this.container, id)
170 }
171
172 /**
173 * Create an exec instance in a container
174 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/create-a-container
175 * @param {Object} opts Query params in the request (optional)
176 * @return {Promise} Promise return the new exec instance
177 */
178 create (opts?: Object): Promise<Exec> {
179 const call = {
180 path: `/containers/${this.container.id}/exec?`,
181 method: 'POST',
182 options: opts,
183 statusCodes: {
184 200: true,
185 201: true,
186 404: 'no such container',
187 409: 'container is paused',
188 500: 'server error'
189 }
190 }
191
192 return new Promise((resolve, reject) => {
193 this.modem.dial(call, (err, conf) => {
194 if (err) return reject(err)
195 const exec = new Exec(this.modem, this.container, conf.Id)
196 exec.data = conf
197 resolve(exec)
198 })
199 })
200 }
201}
202
203/**
204 * Class representing container filesystem
205 */
206export class ContainerFs {
207 modem: Modem
208 container: Container
209
210 /**
211 * Create an container filesystem object
212 * @param {Modem} modem Modem to connect to the remote service
213 * @param {Container} container Container that owns the filesystem (optional)
214 */
215 constructor (modem: Modem, container: Container) {
216 this.modem = modem
217 this.container = container
218 }
219
220 /**
221 * Get the info about the filesystem of the container
222 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/retrieving-information-about-files-and-folders-in-a-container
223 * @param {Object} opts Query params in the request (optional)
224 * @return {Promise} Promise returning the info about the filesystem
225 */
226 info (opts?: Object): Promise<String> {
227 const call = {
228 path: `/containers/${this.container.id}/archive?`,
229 method: 'HEAD',
230 isStream: true,
231 options: opts,
232 statusCodes: {
233 200: true,
234 404: 'bad request',
235 500: 'server error'
236 }
237 }
238
239 return new Promise((resolve, reject) => {
240 this.modem.dial(call, (err, info: String) => {
241 if (err) return reject(err)
242 resolve(info)
243 })
244 })
245 }
246
247 /**
248 * Get a tar archive of a resource in the filesystem of a container
249 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/get-an-archive-of-a-filesystem-resource-in-a-container
250 * @param {Object} opts Query params in the request (optional)
251 * @return {Promise} Promise returning the result as a stream to the tar file
252 */
253 get (opts: any = {}): Promise<Object> {
254 const call = {
255 path: `/containers/${this.container.id}/archive?path=${opts.path}&`,
256 method: 'GET',
257 isStream: true,
258 options: opts,
259 statusCodes: {
260 200: true,
261 400: 'bad request',
262 404: 'no such container',
263 500: 'server error'
264 }
265 }
266
267 return new Promise((resolve, reject) => {
268 this.modem.dial(call, (err, stream: Object) => {
269 if (err) return reject(err)
270 resolve(stream)
271 })
272 })
273 }
274
275 /**
276 * Put an extracted tar archive in the filesystem of a container
277 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/extract-an-archive-of-files-or-folders-to-a-directory-in-a-container
278 * @param {Object} opts Query params in the request (optional)
279 * @return {Promise} Promise returning the result
280 */
281 put (file: fs.ReadStream, opts?: Object): Promise<Object> {
282 const call = {
283 path: `/containers/${this.container.id}/archive?`,
284 method: 'PUT',
285 options: opts,
286 isStream: true,
287 file: file,
288 statusCodes: {
289 200: true,
290 400: 'bad request',
291 403: 'permission denied',
292 404: 'no such container',
293 500: 'server error'
294 }
295 }
296
297 return new Promise((resolve, reject) => {
298 this.modem.dial(call, (err, res: Object) => {
299 if (err) return reject(err)
300 resolve(res)
301 })
302 })
303 }
304}
305
306/**
307 * Class representing a container
308 */
309export class Container {
310 modem: Modem
311 id: String
312 fs: ContainerFs
313 exec: ExecManager
314 Warnings: Array<String> = []
315 data: Object = {}
316
317 /**
318 * Create an container object
319 * @param {Modem} modem Modem to connect to the remote service
320 * @param {string} id Container id
321 */
322 constructor (modem: Modem, id: String) {
323 this.modem = modem
324 this.id = id
325 this.fs = new ContainerFs(this.modem, this)
326 this.exec = new ExecManager(this.modem, this)
327 }
328
329 /**
330 * Get low-level information on a container
331 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/inspect-a-container
332 * The reason why this module isn't called inspect is because that interferes with the inspect utility of node.
333 * @param {Object} opts Query params in the request (optional)
334 * @return {Promise} Promise return the container
335 */
336 status (opts?: Object): Promise<Container> {
337 const call = {
338 path: `/containers/${this.id}/json?`,
339 method: 'GET',
340 options: opts,
341 statusCodes: {
342 200: true,
343 404: 'no such container',
344 500: 'server error'
345 }
346 }
347
348 return new Promise((resolve, reject) => {
349 this.modem.dial(call, (err, conf) => {
350 if (err) return reject(err)
351 const container = new Container(this.modem, this.id)
352 container.data = conf
353 resolve(container)
354 })
355 })
356 }
357
358 /**
359 * Get list of processes (ps) inside a container. Not supported in Windows.
360 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/list-processes-running-inside-a-container
361 * @param {Object} opts Query params in the request (optional)
362 * @return {Promise} Promise return the list of processes
363 */
364 top (opts?: Object): Promise<Array<Object>> {
365 const call = {
366 path: `/containers/${this.id}/top?`,
367 method: 'GET',
368 options: opts,
369 statusCodes: {
370 200: true,
371 404: 'no such container',
372 500: 'server error'
373 }
374 }
375
376 return new Promise((resolve, reject) => {
377 this.modem.dial(call, (err, processes: Array<Object>) => {
378 if (err) return reject(err)
379 resolve(processes)
380 })
381 })
382 }
383
384 /**
385 * Get stdout and stderr logs from a container
386 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/get-container-logs
387 * @param {Object} opts Query params in the request (optional)
388 * @return {Promise} Promise returning the concatenated logs
389 */
390 logs (opts?: Object): Promise<Object> {
391 const call = {
392 path: `/containers/${this.id}/logs?`,
393 method: 'GET',
394 options: opts,
395 isStream: true,
396 statusCodes: {
397 101: true,
398 200: true,
399 404: 'no such container',
400 500: 'server error'
401 }
402 }
403
404 return new Promise((resolve, reject) => {
405 this.modem.dial(call, (err, logs: Object) => {
406 if (err) return reject(err)
407 resolve(logs)
408 })
409 })
410 }
411
412 /**
413 * Get changes on a container's filesystem
414 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/inspect-changes-on-a-container-s-filesystem
415 * @return {Promise} Promise returning the changes
416 */
417 changes (): Promise<Array<Object>> {
418 const call = {
419 path: `/containers/${this.id}/changes?`,
420 method: 'GET',
421 options: {},
422 statusCodes: {
423 200: true,
424 404: 'no such container',
425 500: 'server error'
426 }
427 }
428
429 return new Promise((resolve, reject) => {
430 this.modem.dial(call, (err, changes: Array<Object>) => {
431 if (err) return reject(err)
432 resolve(changes)
433 })
434 })
435 }
436
437 /**
438 * Export the content of a container
439 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/export-a-container
440 * @param {Object} opts Query params in the request (optional)
441 * @return {Promise} Promise returning the content of the tar file as a stream or as a string
442 */
443 export (opts: any = {}): Promise<Object> {
444 const call = {
445 path: `/containers/${this.id}/export?`,
446 method: 'GET',
447 options: opts,
448 isStream: !!opts.stream,
449 statusCodes: {
450 200: true,
451 404: 'no such container',
452 500: 'server error'
453 }
454 }
455
456 return new Promise((resolve, reject) => {
457 this.modem.dial(call, (err, tarStream: any) => {
458 if (err) return reject(err)
459 if (!opts.stream) return resolve(tarStream)
460
461 const res = []
462 tarStream.on('data', (chunk) => {
463 res.push(chunk.toString())
464 })
465
466 tarStream.on('end', () => {
467 resolve(res.join(''))
468 })
469 })
470 })
471 }
472
473 /**
474 * Get the stats of a container, either by a live stream or the current state
475 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/export-a-container
476 * @param {Object} opts Query params in the request (optional)
477 * @return {Promise} Promise returning the stats, in a stream or string
478 */
479 stats (opts?: Object): Promise<Object> {
480 const call = {
481 path: `/containers/${this.id}/stats?`,
482 method: 'GET',
483 options: opts,
484 isStream: true,
485 statusCodes: {
486 200: true,
487 404: 'no such container',
488 500: 'server error'
489 }
490 }
491
492 return new Promise((resolve, reject) => {
493 this.modem.dial(call, (err, stats: Object) => {
494 if (err) return reject(err)
495 resolve(stats)
496 })
497 })
498 }
499
500 /**
501 * Resize the TTY for a container. You must restart the container to make the resize take effect.
502 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/resize-a-container-tty
503 * @param {Object} opts Query params in the request (optional)
504 * @return {Promise} Promise returning the response
505 */
506 resize (opts?: Object): Promise<Object> {
507 const call = {
508 path: `/containers/${this.id}/resize?`,
509 method: 'GET',
510 options: opts,
511 statusCodes: {
512 200: true,
513 404: 'no such container',
514 500: 'server error'
515 }
516 }
517
518 return new Promise((resolve, reject) => {
519 this.modem.dial(call, (err, res: Object) => {
520 if (err) return reject(err)
521 resolve(res)
522 })
523 })
524 }
525
526 /**
527 * Start a container
528 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/start-a-container
529 * @param {Object} opts Query params in the request (optional)
530 * @return {Promise} Promise returning the container
531 */
532 start (opts: Object = {}): Promise<Container> {
533 const call = {
534 path: `/containers/${this.id}/start?`,
535 method: 'POST',
536 options: opts,
537 statusCodes: {
538 204: true,
539 304: true,
540 404: 'no such container',
541 500: 'server error'
542 }
543 }
544
545 return new Promise((resolve, reject) => {
546 this.modem.dial(call, (err) => {
547 if (err) return reject(err)
548 resolve(this)
549 })
550 })
551 }
552
553 /**
554 * Stop a container
555 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/stop-a-container
556 * @param {Object} opts Query params in the request (optional)
557 * @return {Promise} Promise returning the container
558 */
559 stop (opts?: Object): Promise<Container> {
560 const call = {
561 path: `/containers/${this.id}/stop?`,
562 method: 'POST',
563 options: opts,
564 statusCodes: {
565 204: true,
566 304: true,
567 404: 'no such container',
568 500: 'server error'
569 }
570 }
571
572 return new Promise((resolve, reject) => {
573 this.modem.dial(call, (err) => {
574 if (err) return reject(err)
575 resolve(this)
576 })
577 })
578 }
579
580 /**
581 * Restart a container
582 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/restart-a-container
583 * @param {Object} opts Query params in the request (optional)
584 * @return {Promise} Promise returning the container
585 */
586 restart (opts?: Object): Promise<Container> {
587 const call = {
588 path: `/containers/${this.id}/restart?`,
589 method: 'POST',
590 options: opts,
591 statusCodes: {
592 204: true,
593 404: 'no such container',
594 500: 'server error'
595 }
596 }
597
598 return new Promise((resolve, reject) => {
599 this.modem.dial(call, (err) => {
600 if (err) return reject(err)
601 resolve(this)
602 })
603 })
604 }
605
606 /**
607 * Kill a container
608 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/kill-a-container
609 * @param {Object} opts Query params in the request (optional)
610 * @return {Promise} Promise returning the container
611 */
612 kill (opts?: Object): Promise<Container> {
613 const call = {
614 path: `/containers/${this.id}/kill?`,
615 method: 'POST',
616 options: opts,
617 statusCodes: {
618 204: true,
619 404: 'no such container',
620 500: 'server error'
621 }
622 }
623
624 return new Promise((resolve, reject) => {
625 this.modem.dial(call, (err) => {
626 if (err) return reject(err)
627 resolve(this)
628 })
629 })
630 }
631
632 /**
633 * Update configuration a container.
634 * Docs says you can do it for more than one, but doesn't exaplin how, so let's leave it in only one
635 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/update-a-container
636 * @param {Object} opts Query params in the request (optional)
637 * @return {Promise} Promise returning the container
638 */
639 update (opts?: Object): Promise<Container> {
640 const call = {
641 path: `/containers/${this.id}/update?`,
642 method: 'POST',
643 options: opts,
644 statusCodes: {
645 200: true,
646 400: 'bad request',
647 404: 'no such container',
648 500: 'server error'
649 }
650 }
651
652 return new Promise((resolve, reject) => {
653 this.modem.dial(call, (err, warnings: Array<String>) => {
654 const container = new Container(this.modem, this.id)
655 container.Warnings = warnings
656 if (err) return reject(err)
657 resolve(container)
658 })
659 })
660 }
661
662 /**
663 * Rename a container.
664 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/rename-a-container
665 * @param {Object} opts Query params in the request (optional)
666 * @return {Promise} Promise returning the container
667 */
668 rename (opts: Object): Promise<Container> {
669 const call = {
670 path: `/containers/${this.id}/rename?`,
671 method: 'POST',
672 options: opts,
673 statusCodes: {
674 204: true,
675 404: 'no such container',
676 409: 'name already taken',
677 500: 'server error'
678 }
679 }
680
681 return new Promise((resolve, reject) => {
682 this.modem.dial(call, (err) => {
683 if (err) return reject(err)
684 resolve(this)
685 })
686 })
687 }
688
689 /**
690 * Pause a container.
691 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/pause-a-container
692 * @return {Promise} Promise returning the container
693 */
694 pause (): Promise<Container> {
695 const call = {
696 path: `/containers/${this.id}/pause?`,
697 method: 'POST',
698 options: {},
699 statusCodes: {
700 204: true,
701 404: 'no such container',
702 500: 'server error'
703 }
704 }
705
706 return new Promise((resolve, reject) => {
707 this.modem.dial(call, (err) => {
708 if (err) return reject(err)
709 resolve(this)
710 })
711 })
712 }
713
714 /**
715 * Unpause a container.
716 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/unpause-a-container
717 * @return {Promise} Promise returning the container
718 */
719 unpause (): Promise<Container> {
720 const call = {
721 path: `/containers/${this.id}/unpause?`,
722 method: 'POST',
723 options: {},
724 statusCodes: {
725 204: true,
726 404: 'no such container',
727 500: 'server error'
728 }
729 }
730
731 return new Promise((resolve, reject) => {
732 this.modem.dial(call, (err) => {
733 if (err) return reject(err)
734 resolve(this)
735 })
736 })
737 }
738
739 /**
740 * Attach to a container.
741 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/attach-to-a-container
742 * @param {Object} opts Query params in the request (optional)
743 * @return {Promise} Promise returning the container
744 */
745 attach (opts: any = {}): Promise<Array<Object>> {
746 const call = {
747 path: `/containers/${this.id}/attach?`,
748 method: 'POST',
749 isStream: true,
750 openStdin: opts.stdin,
751 options: opts,
752 statusCodes: {
753 101: true,
754 200: true,
755 400: 'bad request',
756 404: 'no such container',
757 500: 'server error'
758 }
759 }
760
761 return new Promise((resolve, reject) => {
762 this.modem.dial(call, (err, stream) => {
763 if (err) return reject(err)
764 resolve([ stream, this ])
765 })
766 })
767 }
768
769 /**
770 * Attach to a container using websocket.
771 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/attach-to-a-container-websocket
772 * @param {Object} opts Query params in the request (optional)
773 * @return {Promise} Promise returning the stream and the container
774 */
775 wsattach (opts?: Object): Promise<Array<Object>> {
776 const call = {
777 path: `/containers/${this.id}/attach/ws?`,
778 method: 'GET',
779 options: opts,
780 statusCodes: {
781 200: true,
782 400: 'bad request',
783 404: 'no such container',
784 500: 'server error'
785 }
786 }
787
788 return new Promise((resolve, reject) => {
789 this.modem.dial(call, (err, stream) => {
790 if (err) return reject(err)
791 resolve([ stream, this ])
792 })
793 })
794 }
795
796 /**
797 * Block until a container stops, returning exit code
798 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/wait-a-container
799 * @return {Promise} Promise returning the exit code
800 */
801 wait (): Promise<Number> {
802 const call = {
803 path: `/containers/${this.id}/wait?`,
804 method: 'POST',
805 options: {},
806 statusCodes: {
807 200: true,
808 404: 'no such container',
809 500: 'server error'
810 }
811 }
812
813 return new Promise((resolve, reject) => {
814 this.modem.dial(call, (err, code: Number) => {
815 if (err) return reject(err)
816 resolve(code)
817 })
818 })
819 }
820
821 /**
822 * Remove a container.
823 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/remove-a-container
824 * @param {Object} opts Query params in the request (optional)
825 * @return {Promise} Promise returning nothing
826 */
827 delete (opts?: Object): Promise<{}> {
828 const call = {
829 path: `/containers/${this.id}?`,
830 method: 'DELETE',
831 options: opts,
832 statusCodes: {
833 204: true,
834 400: 'bad request',
835 404: 'no such container',
836 500: 'server error'
837 }
838 }
839
840 return new Promise((resolve, reject) => {
841 this.modem.dial(call, (err) => {
842 if (err) return reject(err)
843 resolve()
844 })
845 })
846 }
847
848 /**
849 * Commit container into an image
850 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/create-a-new-image-from-a-container-s-changes
851 * @param {Object} opts Query params in the request (optional)
852 * @return {Promise} Promise returning the new image
853 */
854 commit (opts: any = {}): Promise<Image> {
855 opts.container = this.id
856
857 const call = {
858 path: `/commit?`,
859 method: 'POST',
860 options: opts,
861 statusCodes: {
862 201: true,
863 404: 'no such container',
864 500: 'server error'
865 }
866 }
867
868 return new Promise((resolve, reject) => {
869 this.modem.dial(call, (err, res) => {
870 if (err) return reject(err)
871 resolve(new Image(this.modem, res.Id.replace('sha256:', '')))
872 })
873 })
874 }
875}
876
877export default class {
878 modem: Modem
879
880 constructor(modem: Modem) {
881 this.modem = modem
882 }
883
884 /**
885 * Get a Container object
886 * @param {id} string ID of the container
887 * @return {Container}
888 */
889 get (id: String): Container {
890 return new Container(this.modem, id)
891 }
892
893 /**
894 * Get the list of containers
895 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/list-containers
896 * @param {Object} opts Query params in the request (optional)
897 * @return {Promise} Promise returning the result as a list of containers
898 */
899 list (opts?: Object): Promise<Array<Container>> {
900 const call = {
901 path: '/containers/json?',
902 method: 'GET',
903 options: opts,
904 statusCodes: {
905 200: true,
906 400: 'bad request',
907 500: 'server error'
908 }
909 }
910
911 return new Promise((resolve, reject) => {
912 this.modem.dial(call, (err, containers) => {
913 if (err) return reject(err)
914 resolve(containers.map((conf) => {
915 const container = new Container(this.modem, conf.Id)
916 container.data = conf
917 return container
918 }))
919 })
920 })
921 }
922
923 /**
924 * Create a container
925 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/create-a-container
926 * @param {Object} opts Query params in the request (optional)
927 * @return {Promise} Promise return the new container
928 */
929 create (opts?: Object): Promise<Container> {
930 const call = {
931 path: '/containers/create?',
932 method: 'POST',
933 options: opts,
934 statusCodes: {
935 200: true,
936 201: true,
937 400: 'bad request',
938 404: 'no such image',
939 406: 'impossible to attach',
940 500: 'server error'
941 }
942 }
943
944 return new Promise((resolve, reject) => {
945 this.modem.dial(call, (err, conf) => {
946 if (err) return reject(err)
947 const container = new Container(this.modem, conf.Id)
948 container.data = conf
949 resolve(container)
950 })
951 })
952 }
953
954 /**
955 * Prune a container
956 * https://docs.docker.com/engine/api/v1.25/#operation/ContainerPrune
957 * @param {Object} opts Query params in the request (optional)
958 * @return {Promise} Promise returning the container
959 */
960 prune (opts?: Object): Promise<Object> {
961 const call = {
962 path: `/containers/prune`,
963 method: 'POST',
964 options: opts,
965 statusCodes: {
966 200: true,
967 500: 'server error'
968 }
969 }
970
971 return new Promise((resolve, reject) => {
972 this.modem.dial(call, (err, res: Object) => {
973 if (err) return reject(err)
974 resolve(res)
975 })
976 })
977 }
978}