-
Notifications
You must be signed in to change notification settings - Fork 11
/
container.ts
132 lines (120 loc) · 3.51 KB
/
container.ts
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
import {
ListContainerResponse,
ContainerCreate,
ContainerCreateResponse, InspectResponse,
} from "./lib/types/container/mod.ts";
import { DockerClient } from "./lib/client/client.ts";
interface ListOptions {
// Return all containers. By default, only running containers are shown
all?: boolean;
// Return this number of most recently created containers, including non-running ones.
limit?: number;
// Return the size of container as fields SizeRw and SizeRootFs.
size?: number;
// Filters to process on the container list, encoded as JSON (a map[string][]string). For example, {"status": ["paused"]} will only return paused containers.
filters?: string;
}
export class Container {
private client: DockerClient;
constructor(client: DockerClient) {
this.client = client;
}
async list(options?: ListOptions): Promise<ListContainerResponse[]> {
const res = await this.client.get("/containers/json", [
{name: "all", value: options?.all ? "true" : ""},
{name: "limit", value: options?.limit ? options.limit.toString() : ""},
{name: "size", value: options?.size ? options.size.toString() : ""},
{name: "filters", value: options?.filters ?? ""},
]);
if (!res.body || !res.body.length) {
return [];
}
return JSON.parse(res.body);
}
async create(
name: string,
config: ContainerCreate,
): Promise<ContainerCreateResponse> {
const res = await this.client.post(
"/containers/create",
JSON.stringify(config),
[{ name: "name", value: name }],
);
if (!res.body || !res.body.length) {
return {};
}
return JSON.parse(res.body);
}
async start(id: string) {
const res = await this.client.post(`/containers/${id}/start`, "");
if (!res.body || !res.body.length) {
return {};
}
return JSON.parse(res.body);
}
async stop(id: string, timeout = 10) {
const res = await this.client.post(
`/containers/${id}/stop`,
"",
[{ name: "t", value: timeout.toString() }],
);
if (!res.body || !res.body.length) {
return {};
}
return JSON.parse(res.body);
}
async restart(id: string, timeout = 10) {
const res = await this.client.post(
`/containers/${id}/restart`,
"",
[{ name: "t", value: timeout.toString() }],
);
if (!res.body || !res.body.length) {
return {};
}
return JSON.parse(res.body);
}
async kill(id: string, signal = "SIGKILL") {
const res = await this.client.post(
`/containers/${id}/restart`,
"",
[{ name: "signal", value: signal }],
);
if (!res.body || !res.body.length) {
return {};
}
return JSON.parse(res.body);
}
async wait(id: string, condition = "not-running") {
const res = await this.client.post(
`/containers/${id}/wait`,
"",
[{ name: "condition", value: condition }],
);
if (!res.body || !res.body.length) {
return {};
}
return JSON.parse(res.body);
}
async rm(id: string, condition = "not-running") {
const res = await this.client.delete(
`/containers/${id}`,
"",
[{ name: "condition", value: condition }],
);
if (!res.body || !res.body.length) {
return {};
}
return JSON.parse(res.body);
}
async inspect(id: string, size = false): Promise<InspectResponse> {
const res = await this.client.get(
`/containers/${id}/json`,
[{name: "size", value: (Boolean(size)).toString()}]
);
if (!res.body) {
return {};
}
return JSON.parse(res.body)
}
}