Skip to content

Commit 294d2c3

Browse files
committed
Added deleteContainer, killContainer, restartContainer, startContainer, stopContainer
1 parent 76c68e8 commit 294d2c3

File tree

3 files changed

+341
-16
lines changed

3 files changed

+341
-16
lines changed

src/client.ts

Lines changed: 140 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'fs';
22
import http from 'http';
33
import net from 'net';
44
import { 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";
66
import { DockerStatsStream, DockerLogStream } from './stream';
77
import { 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

src/index.ts

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import DockerClient from './client';
2-
import { DockerContainer, DockerCreateContainerOptions, DockerCreateContainerResponse, DockerCreateContainerResponseDockerError, DockerGetContainerLogsOptions, DockerGetContainerLogsResponse, DockerGetContainerLogsResponseError, DockerGetContainersOptions, DockerGetContainerStatsOptions, DockerGetContainerStatsResponseError, DockerGetContainerStatsResult, DockerResult } from './types';
2+
import { DockerContainer, DockerCreateContainerOptions, DockerCreateContainerResponse, DockerCreateContainerResponseDockerError, DockerDeleteContainerOptions, DockerDeleteContainerResponseError, DockerGetContainerLogsOptions, DockerGetContainerLogsResponse, DockerGetContainerLogsResponseError, DockerGetContainersOptions, DockerGetContainerStatsOptions, DockerGetContainerStatsResponseError, DockerGetContainerStatsResult, DockerKillContainerResponseError, DockerRestartContainerOptions, DockerRestartContainerResponseError, DockerResult, DockerStartContainerResponseError, DockerStopContainerOptions, DockerStopContainerResponseError } from './types';
33

44
const DOCKER = new DockerClient();
55

@@ -16,6 +16,18 @@ export async function createContainer(image: string, options?: DockerCreateConta
1616
}
1717

1818

19+
/**
20+
* Deletes a container.
21+
*
22+
* @param containerIdOrName ID or name of the container to delete.
23+
* @param options Optional options for deleting the container.
24+
* @returns Empty object on success or an error.
25+
*/
26+
export async function deleteContainer(containerIdOrName: string, options?: DockerDeleteContainerOptions): Promise<DockerResult<{}, DockerDeleteContainerResponseError>> {
27+
return DOCKER.deleteContainer(containerIdOrName, options);
28+
}
29+
30+
1931
/**
2032
* Returns the logs of a container either as string or as stream.
2133
*
@@ -51,10 +63,65 @@ export async function getContainerStats(containerIdOrName: string, options?: Doc
5163
}
5264

5365

66+
/**
67+
* Kills a container or sends another signal to the container.
68+
* Killing a container is similar to stopping it except that it immediately terminates the container's processes.
69+
*
70+
* @param containerIdOrName ID or name of the container.
71+
* @param signal Optional signal to send to the container.
72+
* @returns Promise resolving to empty object on successful kill or an error.
73+
*/
74+
export async function killContainer(containerIdOrName: string, signal?: number | string): Promise<DockerResult<{}, DockerKillContainerResponseError>> {
75+
return DOCKER.killContainer(containerIdOrName, signal);
76+
}
77+
78+
79+
/**
80+
* Restarts a container.
81+
*
82+
* @param containerIdOrName ID or name of the container.
83+
* @param options Optional parameters for restarting the container.
84+
* @returns Promise resolving to empty object on successful restart or an error.
85+
*/
86+
export async function restartContainer(containerIdOrName: string, options?: DockerRestartContainerOptions): Promise<DockerResult<{}, DockerRestartContainerResponseError>> {
87+
return DOCKER.restartContainer(containerIdOrName, options);
88+
}
89+
90+
91+
/**
92+
* Starts a container.
93+
*
94+
* @param containerIdOrName ID or name of the container.
95+
* @param detachKeys Optional key sequence for detaching the container. Single character [a-Z] or ctrl-<value> with <value> being [a-z], '@', '^', ',', '[', or '_'.
96+
* @returns Promise resolving to empty object on successful start or an error.
97+
*/
98+
export async function startContainer(containerIdOrName: string, detachKeys?: string): Promise<DockerResult<{}, DockerStartContainerResponseError>> {
99+
return DOCKER.startContainer(containerIdOrName, detachKeys);
100+
}
101+
102+
103+
/**
104+
* Stops a running container.
105+
*
106+
* @param containerIdOrName ID or name of the container.
107+
* @param options Optional parameters for stopping the container.
108+
* @returns Promise resolving to empty object on successful stop or an error.
109+
*/
110+
export async function stopContainer(containerIdOrName: string, options?: DockerStopContainerOptions): Promise<DockerResult<{}, DockerStopContainerResponseError>> {
111+
return DOCKER.stopContainer(containerIdOrName, options);
112+
}
113+
54114

55115
const Docker = {
56116
DockerClient,
57117
createContainer,
118+
deleteContainer,
119+
getContainerLogs,
58120
getContainers,
121+
getContainerStats,
122+
killContainer,
123+
restartContainer,
124+
startContainer,
125+
stopContainer,
59126
};
60127
export default Docker;

0 commit comments

Comments
 (0)