-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker.ts
More file actions
71 lines (62 loc) · 2.62 KB
/
docker.ts
File metadata and controls
71 lines (62 loc) · 2.62 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
64
65
66
67
68
69
70
71
import Docker from "dockerode";
import { spawn } from "child_process";
import { mkdir } from "fs/promises";
export const docker = new Docker({ socketPath: "/var/run/docker.sock" });
export const FEDERATED_DOCKER_HUB_REPOSITORY = process.env.FEDERATED_DOCKER_HUB_REPOSITORY || "codestix/federated";
export const JUPYTER_DOCKER_HUB_REPOSITORY = process.env.JUPYTER_DOCKER_HUB_REPOSITORY || "tensorflow/tensorflow";
export function getRandomPort() {
// Pick random port, could be already taken but chance is very small
return Math.floor(Math.random() * 50000 + 10000);
}
export async function createFederatedContainer(port: number, gpus: number[] = [], version: string = "0.23.0") {
// As specified in https://github.com/tensorflow/federated/blob/main/tensorflow_federated/tools/runtime/remote_executor_service.py
const INTERNAL_PORT = 8000;
// See https://docs.docker.com/engine/api/v1.37/#operation/ContainerCreate
return await docker.createContainer({
Image: FEDERATED_DOCKER_HUB_REPOSITORY + ":" + version,
HostConfig: {
PortBindings: {
[INTERNAL_PORT + "/tcp"]: [
{
HostPort: String(port),
HostIp: "0.0.0.0",
},
],
},
},
});
}
export async function createJupyterContainer(port: number, token: string, version: string = "latest-gpu-jupyter") {
const INTERNAL_PORT = 8888;
// TODO: pass GPUs
return await docker.createContainer({
Image: JUPYTER_DOCKER_HUB_REPOSITORY + ":" + version,
// This command is the entrypoint command in the tensorflow/tensorflow image and it sets the notebook token according to https://stackoverflow.com/questions/47092878/auto-configure-jupyter-password-from-command-line
Cmd: [
"bash",
"-c",
`source /etc/bash.bashrc && jupyter notebook --notebook-dir=/tf --ip 0.0.0.0 --no-browser --allow-root --NotebookApp.token='${token}'`,
],
Labels: {
Type: "task", // Contains user-defined labels
TaskType: "jupyter",
},
HostConfig: {
PortBindings: {
[INTERNAL_PORT + "/tcp"]: [
{
HostPort: String(port),
HostIp: "0.0.0.0",
},
],
},
},
});
}
export async function removeContainer(container: Docker.Container) {
let inspectContainer = await container.inspect();
if (inspectContainer.State.Running) {
await container.stop();
}
await container.remove();
}