-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathdocker_controller.py
More file actions
81 lines (71 loc) · 2.98 KB
/
Copy pathdocker_controller.py
File metadata and controls
81 lines (71 loc) · 2.98 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
72
73
74
75
76
77
78
79
80
81
import subprocess
if __name__ == "__main__":
# debug stand alone to find meshio in the plugin path, instead site-packages
import os
plugin_site_packages=os.path.join(os.path.dirname(os.path.abspath(__file__)), "site-packages","src")
import sys
sys.path.append(plugin_site_packages)
def isDockerAvailable():
# if docker is installed and available
try:
# execute "docker --version" and return the output
result1 = subprocess.run(["docker", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result = result1.stdout.decode().strip()
return result
except (subprocess.CalledProcessError, FileNotFoundError):
return -1
return -1
def isDockerRunning():
# if docker is running
try:
result1 = subprocess.run(["docker", "info"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# get the output and check if it contains "Server Version"
result = result1.stdout.decode().strip()
return result
except (subprocess.CalledProcessError, FileNotFoundError):
return -1
return -1
# is docker running any container for an image
def isDockerRunningContainer(image_name, external_port=-1):
try:
args = ["docker", "ps", "-q", "--filter", f"ancestor={image_name}"]
if external_port != -1:
args.extend(["--filter", f"publish={external_port}"])
args.extend(["--format", "{{.ID}}"])
result = subprocess.run(
args,
check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
container_ids = result.stdout.decode().strip().split('\n')
if container_ids == ['']:
return 0
return len(container_ids)
except (subprocess.CalledProcessError, FileNotFoundError):
return False
return False
def killContainersFromImage(image_name, external_port=-1):
try:
args = ["docker", "ps", "-q", "--filter", f"ancestor={image_name}"]
if external_port != -1:
args.extend(["--filter", f"publish={external_port}"])
args.extend(["--format", "{{.ID}}"])
result = subprocess.run(
args,
check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
)
container_ids = result.stdout.strip().splitlines()
if not container_ids:
return 0
result = subprocess.run(["docker", "rm", "-f"] + container_ids, check=True)
return len(container_ids)
except subprocess.CalledProcessError as e:
return -1
def startContainerForImage(image_name, external_port, internal_port, modelname):
try:
result = subprocess.run(
["docker", "run", "-d", "-p", f"{external_port}:{internal_port}", "-v", f"{modelname}:/model", image_name],
check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
)
container_id = result.stdout.strip()
return container_id
except subprocess.CalledProcessError as e:
return -1