-
Notifications
You must be signed in to change notification settings - Fork 2
/
docker.js
71 lines (56 loc) · 1.71 KB
/
docker.js
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
import path from "path"
import assert from "assert"
export class DockerBuilder {
constructor(container_name, image_name, command) {
assert(container_name && image_name && command);
this._container_name = container_name;
this._image_name = image_name;
this._command = command;
this._ports = []
this._volumes = []
this._environment = {}
this._container_uid = undefined
this._container_gid = undefined
}
addVolume(src, dst) {
assert(src && dst);
this._volumes.push({"abs_source" : path.resolve(src),
"source" : src,
"destination" : dst});
}
setEnv(key, value) {
assert(key && value);
this._environment[key] = value;
}
setCreds(uid, gid) {
assert(typeof(uid) == 'number' && typeof(gid) == 'number');
this._container_uid = uid;
this._container_gid = gid;
}
publishUDP(port) {
assert(typeof(port) == 'number' && port > 0 && port < 65536);
this._ports.push({"type" : "udp", "port" : port});
}
publishTCP(port) {
assert(typeof(port) == 'number' && port > 0 && port < 65536);
this._ports.push({"type" : "tcp", "port" : port});
}
generateRunCommand() {
let commands = ["docker", "run", "--detach", "--rm"];
commands.push("--name", this._container_name);
this._ports.forEach(p => {
commands.push("--publish", `${p["port"]}:${p["port"]}/${p["type"]}`);
});
if (this._container_uid)
commands.push("--user", `${this._container_uid}:${this._container_gid}`);
this._volumes.forEach(v => {
commands.push("--volume", `${v["abs_source"]}:${v["destination"]}`);
});
for (const key in this._environment) {
commands.push("--env", `${key}=${this._environment[key]}`);
}
commands.push(this._image_name);
commands.push(this._command);
return commands;
}
}