-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-manager.js
More file actions
63 lines (56 loc) · 1.81 KB
/
docker-manager.js
File metadata and controls
63 lines (56 loc) · 1.81 KB
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
const Docker = require("dockerode");
const short = require("short-uuid");
const uuid = short();
module.exports = class DockerManager {
constructor() {
this.docker = new Docker();
}
getContainerName() {
const containerUUID = uuid.new();
return `try-package-${containerUUID}`;
}
async createContainer() {
this.containerName = this.getContainerName();
this.container = await this.docker.createContainer({
Image: "my-compiler",
name: this.containerName,
Binds: [`${process.cwd()}/project:/project`],
Tty: true,
OpenStdin: false,
AttachStdin: true,
WorkingDir: "/",
StopTimeout: 7
});
await this.container.start();
return this.containerName;
}
async execute(command) {
const exec = await this.container.exec({
Cmd: command,
AttachStdin: false,
AttachStdout: true,
AttachStderr: true
});
return new Promise(async (resolve, reject) => {
await exec.start(async (err, stream) => {
if (err) return reject();
let message = "";
stream.on("data", data => {
message += data.toLocaleString();
return message;
});
stream.on("end", () => resolve(message));
});
});
}
async stop() {
return await this.container.kill();
}
async inspect() {
const data = await this.container.inspect();
return data;
}
removeContainer() {
return this.container.remove({ force: true });
}
};