@@ -2,7 +2,7 @@ import fs from 'fs';
22import http from 'http' ;
33import net from 'net' ;
44import { Readable } from 'stream' ;
5- import { DockerClientOptions , DockerContainer , DockerContainerStats , DockerCreateContainerOptions , DockerCreateContainerResponse , DockerCreateContainerResponseDockerError , DockerError , DockerGetContainerLogsOptions , DockerGetContainerLogsResponse , DockerGetContainerLogsResponseError , DockerGetContainerLogsResponseStream , DockerGetContainersOptions , DockerGetContainerStatsOptions , DockerGetContainerStatsResponseError , DockerGetContainerStatsResult , DockerResult , DockerSuccess } from "./types" ;
5+ import { DockerClientOptions , DockerContainer , DockerContainerStats , DockerCreateContainerOptions , DockerCreateContainerResponse , DockerCreateContainerResponseDockerError , DockerDeleteContainerOptions , DockerDeleteContainerResponseError , DockerError , DockerGetContainerLogsOptions , DockerGetContainerLogsResponse , DockerGetContainerLogsResponseError , DockerGetContainerLogsResponseStream , DockerGetContainersOptions , DockerGetContainerStatsOptions , DockerGetContainerStatsResponseError , DockerGetContainerStatsResult , DockerKillContainerResponseError , DockerRestartContainerOptions , DockerRestartContainerResponseError , DockerResult , DockerStartContainerResponseError , DockerStopContainerOptions , DockerStopContainerResponseError , DockerSuccess } from "./types" ;
66import { DockerStatsStream , DockerLogStream } from './stream' ;
77import { decodeDockerContainer , decodeDockerContainerStats , encodeCreateContainerOptions } from './convert' ;
88
@@ -109,7 +109,7 @@ class DockerClient {
109109 * @param stream Optional if the body should be interpreted as a stream.
110110 * @returns Response object.
111111 */
112- public async request ( method : 'GET' | 'POST' , uri : string , query ?: { [ key : string ] : string } , body ?: string , stream ?: boolean ) : Promise < DockerRequestResult > {
112+ public async request ( method : 'GET' | 'POST' | 'DELETE' , uri : string , query ?: { [ key : string ] : string } , body ?: string , stream ?: boolean ) : Promise < DockerRequestResult > {
113113 await this . checkReady ( ) ;
114114
115115 const queryArr = Object . entries ( query || { } ) . map < string > ( entry => entry [ 0 ] + '=' + encodeURIComponent ( entry [ 1 ] ) ) ;
@@ -212,7 +212,7 @@ class DockerClient {
212212 * @returns Response object.
213213 * @template T Return type.
214214 */
215- public async requestJson < T = any > ( method : 'GET' | 'POST' , uri : string , query ?: { [ key : string ] : string } , body ?: string ) : Promise < Partial < DockerError > & { status : number , ok : boolean , body ?: T } > {
215+ public async requestJson < T = any > ( method : 'GET' | 'POST' | 'DELETE' , uri : string , query ?: { [ key : string ] : string } , body ?: string ) : Promise < Partial < DockerError > & { status : number , ok : boolean , body ?: T } > {
216216 return this . request ( method , uri , query , body ) . then ( res => {
217217 if ( ! res . ok ) return res as any ;
218218 return { ...res , body : JSON . parse ( ( res as DockerRequestResultBody ) . body ! ) } ;
@@ -233,7 +233,6 @@ class DockerClient {
233233
234234
235235
236-
237236 /**
238237 * Creates a Docker container.
239238 *
@@ -269,6 +268,35 @@ class DockerClient {
269268
270269
271270
271+ /**
272+ * Deletes a container.
273+ *
274+ * @param containerIdOrName ID or name of the container to delete.
275+ * @param options Optional options for deleting the container.
276+ * @returns Empty object on success or an error.
277+ */
278+ public async deleteContainer ( containerIdOrName : string , options ?: DockerDeleteContainerOptions ) : Promise < DockerResult < { } , DockerDeleteContainerResponseError > > {
279+ const query : { [ key : string ] : string } = { } ;
280+ if ( options ?. force ) query . force = 'true' ;
281+ if ( options ?. link ) query . link = 'true' ;
282+ if ( options ?. volumes ) query . v = 'true' ;
283+ return this . requestJson ( 'DELETE' , '/containers/' + containerIdOrName , query ) . then < DockerResult < { } , DockerDeleteContainerResponseError > > ( response => {
284+ if ( ! response . ok || response . body . message ) {
285+ const res : DockerResult < { } , DockerDeleteContainerResponseError > = {
286+ error : response . error !
287+ } ;
288+ if ( response . status === 404 ) res . error ! . notFound = true ;
289+ if ( response . status === 409 ) res . error ! . stillRunning = true ;
290+ return res ;
291+ }
292+
293+ return {
294+ success : { }
295+ } ;
296+ } ) ;
297+ }
298+
299+
272300
273301 /**
274302 * Returns the logs of a container either as string or as stream.
@@ -308,7 +336,6 @@ class DockerClient {
308336
309337
310338
311-
312339 /**
313340 * Returns a list of containers.
314341 *
@@ -354,6 +381,7 @@ class DockerClient {
354381 }
355382
356383
384+
357385 /**
358386 * Returns the utilization stats for a container as single value or as continuous stream.
359387 *
@@ -394,8 +422,115 @@ class DockerClient {
394422 }
395423
396424
425+ /**
426+ * Kills a container or sends another signal to the container.
427+ * Killing a container is similar to stopping it except that it immediately terminates the container's processes.
428+ *
429+ * @param containerIdOrName ID or name of the container.
430+ * @param signal Optional signal to send to the container.
431+ * @returns Promise resolving to empty object on successful kill or an error.
432+ */
433+ public async killContainer ( containerIdOrName : string , signal ?: number | string ) : Promise < DockerResult < { } , DockerKillContainerResponseError > > {
434+ const query : { [ key : string ] : string } = { } ;
435+ if ( signal ) query . signal = signal . toString ( ) ;
436+ return this . requestJson ( 'POST' , '/containers/' + containerIdOrName + '/kill' , query ) . then < DockerResult < { } , DockerKillContainerResponseError > > ( response => {
437+ if ( ! response . ok || response . body . message ) {
438+ const res : DockerResult < { } , DockerKillContainerResponseError > = {
439+ error : response . error !
440+ } ;
441+ if ( response . status === 404 ) res . error ! . notFound = true ;
442+ if ( response . status === 409 ) res . error ! . notRunning = true ;
443+ return res ;
444+ }
445+
446+ return {
447+ success : { }
448+ } ;
449+ } ) ;
450+ }
451+
452+
453+ /**
454+ * Restarts a container.
455+ *
456+ * @param containerIdOrName ID or name of the container.
457+ * @param options Optional parameters for restarting the container.
458+ * @returns Promise resolving to empty object on successful restart or an error.
459+ */
460+ public async restartContainer ( containerIdOrName : string , options ?: DockerRestartContainerOptions ) : Promise < DockerResult < { } , DockerRestartContainerResponseError > > {
461+ const query : { [ key : string ] : string } = { } ;
462+ if ( options ?. signal ) query . signal = options . signal . toString ( ) ;
463+ if ( options ?. timeout !== undefined ) query . t = options . timeout . toString ( ) ;
464+ return this . requestJson ( 'POST' , '/containers/' + containerIdOrName + '/restart' , query ) . then < DockerResult < { } , DockerRestartContainerResponseError > > ( response => {
465+ if ( ! response . ok || response . body . message ) {
466+ const res : DockerResult < { } , DockerRestartContainerResponseError > = {
467+ error : response . error !
468+ } ;
469+ if ( response . status === 404 ) res . error ! . notFound = true ;
470+ return res ;
471+ }
472+
473+ return {
474+ success : { }
475+ } ;
476+ } ) ;
477+ }
478+
479+
480+ /**
481+ * Starts a container.
482+ *
483+ * @param containerIdOrName ID or name of the container.
484+ * @param detachKeys Optional key sequence for detaching the container. Single character [a-Z] or ctrl-<value> with <value> being [a-z], '@', '^', ',', '[', or '_'.
485+ * @returns Promise resolving to empty object on successful start or an error.
486+ */
487+ public async startContainer ( containerIdOrName : string , detachKeys ?: string ) : Promise < DockerResult < { } , DockerStartContainerResponseError > > {
488+ const query : { [ key : string ] : string } = { } ;
489+ if ( detachKeys ) query . detachKeys = detachKeys ;
490+ return this . requestJson ( 'POST' , '/containers/' + containerIdOrName + '/start' , query ) . then < DockerResult < { } , DockerStartContainerResponseError > > ( response => {
491+ if ( ! response . ok || response . body . message ) {
492+ const res : DockerResult < { } , DockerStartContainerResponseError > = {
493+ error : response . error !
494+ } ;
495+ if ( response . status === 304 ) res . error ! . alreadyStarted = true ;
496+ if ( response . status === 404 ) res . error ! . notFound = true ;
497+ return res ;
498+ }
499+
500+ return {
501+ success : { }
502+ } ;
503+ } ) ;
504+ }
505+
506+
397507
508+ /**
509+ * Stops a running container.
510+ *
511+ * @param containerIdOrName ID or name of the container.
512+ * @param options Optional parameters for stopping the container.
513+ * @returns Promise resolving to empty object on successful stop or an error.
514+ */
515+ public async stopContainer ( containerIdOrName : string , options ?: DockerStopContainerOptions ) : Promise < DockerResult < { } , DockerStopContainerResponseError > > {
516+ const query : { [ key : string ] : string } = { } ;
517+ if ( options ?. signal ) query . signal = options . signal . toString ( ) ;
518+ if ( options ?. timeout !== undefined ) query . t = options . timeout . toString ( ) ;
519+ return this . requestJson ( 'POST' , '/containers/' + containerIdOrName + '/stop' , query ) . then < DockerResult < { } , DockerStopContainerResponseError > > ( response => {
520+ if ( ! response . ok || response . body . message ) {
521+ const res : DockerResult < { } , DockerStopContainerResponseError > = {
522+ error : response . error !
523+ } ;
524+ if ( response . status === 304 ) res . error ! . alreadyStopped = true ;
525+ if ( response . status === 404 ) res . error ! . notFound = true ;
526+ return res ;
527+ }
398528
529+ return {
530+ success : { }
531+ } ;
532+ } ) ;
533+ }
399534
400535
401536
0 commit comments