-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_kernels.py
80 lines (65 loc) · 1.91 KB
/
docker_kernels.py
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
#!/usr/bin/python
import argparse
import json
from pathlib import Path
import docker, sys
import traceback
def set_connection_ip(connection_file, ip: str = "0.0.0.0"):
connection = json.loads(Path(connection_file).read_text())
connection["ip"] = ip
connection["kernel_name"] = "my_custom_kernel"
Path(connection_file).write_text(json.dumps(connection))
return connection
parser = argparse.ArgumentParser(description="Running new container")
parser.add_argument("connection_file")
parser.add_argument("my_container")
args = parser.parse_args()
try:
set_connection_ip(args.connection_file)
data = json.loads(Path(args.connection_file).read_text())
except ValueError:
print("Decoding JSON has failed")
ex_type, ex, tb = sys.exc_info()
traceback.print_tb(tb)
exit()
except Exception as err:
print("Error reading connection_file:", err)
ex_type, ex, tb = sys.exc_info()
traceback.print_tb(tb)
exit()
image_name = str(args.my_container)
ports = [
data["shell_port"],
data["iopub_port"],
data["control_port"],
data["stdin_port"],
data["hb_port"],
]
port_mapping = {}
for i in ports:
port_mapping[i] = i
env_vars = {}
CONTAINER_CONNECTION_SPEC_PATH = "/kernel-connection-spec.json"
CONTAINER_CONNECTION_SPEC_ENV_VAR = "DOCKERNEL_CONNECTION_FILE"
client = docker.from_env()
containers = client.containers
connection_file_mount = docker.types.Mount(
target=CONTAINER_CONNECTION_SPEC_PATH,
source=str(args.connection_file),
type="bind",
read_only=False,
)
try:
containers.run(
image=image_name,
tty=True,
command=f"python -m ipykernel_launcher -f {CONTAINER_CONNECTION_SPEC_PATH}",
auto_remove=True,
environment=env_vars,
mounts=[connection_file_mount],
ports=port_mapping,
stdout=True,
stderr=True,
)
except Exception as err:
print("Error running the container:", err)