2
2
3
3
import subprocess
4
4
import os
5
+ import pathlib
6
+ import shutil
5
7
6
8
# To enable compatiblity with Python<=3.6 (e.g. for sinergym dockerfile)
7
9
try :
16
18
def build_experiment_container (
17
19
build_context : str ,
18
20
use_no_cache : bool = False ,
19
- version = "latest" ,
20
- enable_rllib = False ,
21
+ local_dir : pathlib .Path = None ,
22
+ version : str = "latest" ,
23
+ enable_rllib : bool = False ,
21
24
) -> None :
22
25
"""Build experiment container from beobench/integrations/boptest/Dockerfile.
23
26
@@ -27,26 +30,49 @@ def build_experiment_container(
27
30
of existing beobench integration (e.g. `boptest`). See the official docs
28
31
https://docs.docker.com/engine/reference/commandline/build/ for more info.
29
32
use_no_cache (bool, optional): wether to use cache in build. Defaults to False.
33
+ version (str, optional): version to add to container tag. Defaults to "latest".
34
+ enable_rllib (bool, optional): whether to install rllib. Defaults to False.
30
35
"""
31
36
32
37
# Flags are shared between gym image build and gym_and_beobench image build
33
38
flags = []
39
+
40
+ # Using buildx to enable platform-specific builds
41
+ build_commands = ["docker" , "buildx" , "build" ]
42
+
43
+ # On arm64 machines force experiment containers to be amd64
44
+ # This is only useful for development purposes.
45
+ # (example: M1 macbooks)
46
+ if os .uname ().machine in ["arm64" , "aarch64" ]:
47
+ flags += ["--platform" , "linux/amd64" ]
48
+
34
49
if use_no_cache :
35
50
flags .append ("--no-cache" )
36
51
52
+ # pylint: disable=invalid-name
37
53
AVAILABLE_INTEGRATIONS = [
38
54
"boptest" ,
39
55
"sinergym" ,
40
56
"energym" ,
41
- ] # pylint: disable=invalid-name
57
+ ]
42
58
43
59
if build_context in AVAILABLE_INTEGRATIONS :
44
60
image_name = f"beobench_{ build_context } "
45
61
integration_name = build_context
46
- build_context = (
47
- f"https://github.com/rdnfn/"
48
- f"beobench_contrib.git#main:gyms/{ build_context } "
62
+
63
+ # TODO: remove tmp git dir once buildkit version in docker cli updated
64
+ tmp_git_dir = (local_dir / "tmp" / "beobench_contrib" ).absolute ()
65
+ subprocess .check_call (
66
+ [
67
+ "git" ,
68
+ "clone" ,
69
+ "https://github.com/rdnfn/beobench_contrib.git" ,
70
+ tmp_git_dir ,
71
+ ]
49
72
)
73
+
74
+ build_context = str (tmp_git_dir / "gyms" / integration_name )
75
+
50
76
print (
51
77
(
52
78
f"Recognised integration named { integration_name } : using build"
@@ -64,17 +90,14 @@ def build_experiment_container(
64
90
65
91
# Part 1: build base experiment image
66
92
args = [
67
- "docker" ,
68
- "build" ,
93
+ * build_commands ,
69
94
"-t" ,
70
95
base_image_tag ,
71
- "-f" ,
72
- "Dockerfile" , # change to non-default name
73
96
* flags ,
74
97
build_context ,
75
98
]
76
99
env = os .environ .copy ()
77
- env [ "DOCKER_BUILDKIT" ] = "0"
100
+ print ( "Running command: " + " " . join ( args ))
78
101
subprocess .check_call (
79
102
args ,
80
103
env = env , # this enables accessing dockerfile in subdir
@@ -92,14 +115,13 @@ def build_experiment_container(
92
115
# Which extras to install beobench container
93
116
# e.g. using pip install beobench[extras]
94
117
if enable_rllib :
95
- beobench_extras = ' "extended,rllib"'
118
+ beobench_extras = "extended,rllib"
96
119
else :
97
120
beobench_extras = "extended"
98
121
# Load dockerfile into pipe
99
122
with subprocess .Popen (["cat" , complete_dockerfile ], stdout = subprocess .PIPE ) as proc :
100
123
beobench_build_args = [
101
- "docker" ,
102
- "build" ,
124
+ * build_commands ,
103
125
"-t" ,
104
126
complete_image_tag ,
105
127
"-f" ,
@@ -118,6 +140,8 @@ def build_experiment_container(
118
140
env = env , # this enables accessing dockerfile in subdir
119
141
)
120
142
143
+ # TODO: remove tmp git dir once buildkit version in docker cli updated
144
+ shutil .rmtree (tmp_git_dir )
121
145
print ("Experiment gym image build finished." )
122
146
123
147
return complete_image_tag
0 commit comments