-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild_image_and_run.py
More file actions
165 lines (155 loc) · 5.36 KB
/
build_image_and_run.py
File metadata and controls
165 lines (155 loc) · 5.36 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import os
import argparse
import subprocess
import json
import time
from typing import Optional
def get_args_parser():
parser = argparse.ArgumentParser()
parser.add_argument(
"--dockerfiles_folder",
type=str,
default=os.path.join(os.path.dirname(__file__), ".."),
)
parser.add_argument(
"--tag",
type=str,
default="latest",
)
parser.add_argument(
"--image_name",
type=str,
default=None,
)
parser.add_argument(
"--mode",
type=str,
default="release",
choices=["release", "data", "trainings", "analysis", "it"],
help="Mode of the project to run the container for. \n"
"- release: This mode is responsible for running the __main__.py of the current package. \n"
"- it: This is a development mode that is responsible for running the container in the interactive mode.",
)
parser.add_argument(
"--args",
type=str,
default=None,
help="Arguments to pass to the container as a JSON string "
)
parser.add_argument(
"--only_run",
action=argparse.BooleanOptionalAction,
default=False,
help="If set, only runs the container without building the image.",
)
parser.add_argument(
"--only_build",
action=argparse.BooleanOptionalAction,
default=False,
help="If set, only build the container without running the image.",
)
return parser
def build_images(
*,
dockerfiles_folder: str = ".",
tag: str = "latest",
image_name: Optional[str] = None,
):
if not image_name:
image_name = os.path.basename(os.path.dirname(os.path.dirname(__file__)))
images = []
for root, dirs, files in os.walk(dockerfiles_folder):
for file in files:
if not file.startswith("Dockerfile"):
continue
post_name = file.replace("Dockerfile", "")
full_image_name = f"{image_name}{post_name}:{tag}"
print(f"Building image {full_image_name} from {os.path.join(root, file)}")
cmd = f"docker build -t {full_image_name} -f {os.path.join(root, file)} ."
print(f"{cmd = }")
subprocess.run(cmd, shell=True, check=True, cwd=root)
print(f"Built image {full_image_name}")
images.append(full_image_name)
print(f"Pruning old images")
cmd = "docker image prune --force"
print(f"{cmd = }")
subprocess.run(cmd, shell=True, check=True)
print(f"Pruned old images")
save_images_folder = os.path.join(os.path.dirname(__file__), "..", "data", "dockerfiles")
os.makedirs(save_images_folder, exist_ok=True)
for image in images:
savefile = os.path.abspath(os.path.join(save_images_folder, image.replace(":", "-") + ".tar"))
print(f"Saving {image} to {savefile}")
save_cmd = f"docker save -o {savefile} {image}"
subprocess.run(save_cmd, shell=True, check=True)
print(f"{image} saved to {savefile}.")
return images
def run_container(
*,
container_name: str = None,
image_name: str = None,
tag: str = "latest",
args: list = None,
run_args: list = None,
):
root_folder_name = os.path.basename(os.path.dirname(os.path.dirname(__file__)))
if not container_name:
container_name = f"{image_name}-container"
if not image_name:
image_name = root_folder_name
if ":" in image_name:
image_name, tag = image_name.split(":")
print(f"Removing container {container_name} if it exists")
cmd = f"docker rm --force {container_name}"
print(f"{cmd = }")
subprocess.run(cmd, shell=True, check=False)
print(f"Removed container {container_name}")
time.sleep(1)
print("-" * 80)
print(f"Running container {container_name} from image {image_name}:{tag}")
container_args = [
"--name", container_name,
"--gpus all",
"--detach",
f"--volume="
f"{os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'data'))}"
f":/{root_folder_name}/data",
]
run_args_str = " ".join([str(arg) for arg in run_args]) if run_args else ""
cmd = f"docker run {' '.join(container_args)} {run_args_str} {image_name}:{tag}"
if args:
cmd += " " + " ".join([str(arg) for arg in args])
print(f"{cmd = }")
subprocess.run(cmd, shell=True, check=True)
print(f"Container {container_name} is now running in the background.")
return
def main():
args = get_args_parser().parse_args()
print(f"Running with args: {json.dumps(vars(args), indent=4)}")
if args.only_run:
images = [f"{args.image_name}:{args.tag}"]
else:
images = build_images(
dockerfiles_folder=args.dockerfiles_folder,
tag=args.tag,
image_name=args.image_name,
)
if args.only_build:
return 0
for image in images:
container_img_part = (
image
.replace("/", "-")
.replace(":", "-")
.replace(".", "-")
)
run_container(
image_name=image,
tag=args.tag,
args=json.loads(args.args) if args.args else None,
container_name=f"{container_img_part}-{args.mode}",
run_args=["-e", f"MODE={args.mode}"],
)
return 0
if __name__ == "__main__":
exit(main())