Skip to content

Commit 2ec3673

Browse files
Alexander Jipaazzhipa
Alexander Jipa
andauthored
feat: add privileged option to local_docker (#897) (#898)
Co-authored-by: Alexander Jipa <[email protected]>
1 parent 20d51ce commit 2ec3673

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

torchx/schedulers/docker_scheduler.py

+9
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ def ensure_network(client: Optional["DockerClient"] = None) -> None:
124124
class DockerOpts(TypedDict, total=False):
125125
copy_env: Optional[List[str]]
126126
env: Optional[Dict[str, str]]
127+
privileged: bool
127128

128129

129130
class DockerScheduler(DockerWorkspaceMixin, Scheduler[DockerOpts]):
@@ -287,6 +288,7 @@ def _submit_dryrun(self, app: AppDef, cfg: DockerOpts) -> AppDryRunInfo[DockerJo
287288
LABEL_REPLICA_ID: str(replica_id),
288289
},
289290
"hostname": name,
291+
"privileged": cfg.get("privileged", False),
290292
"network": NETWORK,
291293
"mounts": mounts,
292294
"devices": devices,
@@ -374,6 +376,13 @@ def _run_opts(self) -> runopts:
374376
(e.g. ENV1:v1,ENV2:v2,ENV3:v3 or ENV1:V1;ENV2:V2). Environment variables from env will be applied on top
375377
of the ones from copy_env""",
376378
)
379+
opts.add(
380+
"privileged",
381+
type_=bool,
382+
default=False,
383+
help="If true runs the container with elevated permissions."
384+
" Equivalent to running with `docker run --privileged`.",
385+
)
377386
return opts
378387

379388
def _get_app_state(self, container: "Container") -> AppState:

torchx/schedulers/test/docker_scheduler_test.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def test_submit_dryrun(self) -> None:
7171
app = _test_app()
7272
with patch("torchx.schedulers.docker_scheduler.make_unique") as make_unique_ctx:
7373
make_unique_ctx.return_value = "app_name_42"
74-
info = self.scheduler._submit_dryrun(app, cfg={})
74+
info = self.scheduler.submit_dryrun(app, cfg={})
7575

7676
want = DockerJob(
7777
"app_name_42",
@@ -109,6 +109,7 @@ def test_submit_dryrun(self) -> None:
109109
},
110110
"mem_limit": "3000m",
111111
"shm_size": "3000m",
112+
"privileged": False,
112113
"name": "app_name_42-trainer-0",
113114
"hostname": "app_name_42-trainer-0",
114115
"nano_cpus": int(2e9),
@@ -137,7 +138,7 @@ def test_volume_mounts(self) -> None:
137138
specs.VolumeMount(src="name", dst_path="/tmp", read_only=True),
138139
]
139140

140-
info = self.scheduler._submit_dryrun(app, cfg={})
141+
info = self.scheduler.submit_dryrun(app, cfg={})
141142
want = [
142143
Mount(
143144
target="/tmp",
@@ -154,15 +155,15 @@ def test_device_mounts(self) -> None:
154155
specs.DeviceMount(src_path="foo", dst_path="bar"),
155156
]
156157

157-
info = self.scheduler._submit_dryrun(app, cfg={})
158+
info = self.scheduler.submit_dryrun(app, cfg={})
158159
self.assertEqual(info.request.containers[0].kwargs["devices"], ["foo:bar:rwm"])
159160

160161
def test_resource_devices(self) -> None:
161162
app = _test_app()
162163
app.roles[0].mounts = []
163164
app.roles[0].resource.devices = {"vpc.amazonaws.com/efa": 1}
164165

165-
info = self.scheduler._submit_dryrun(app, cfg={})
166+
info = self.scheduler.submit_dryrun(app, cfg={})
166167
self.assertEqual(
167168
info.request.containers[0].kwargs["devices"],
168169
["/dev/infiniband/uverbs0:/dev/infiniband/uverbs0:rwm"],
@@ -174,7 +175,7 @@ def test_copy_env(self) -> None:
174175
cfg = DockerOpts({"copy_env": ["FOO_*", "BAR_*"]})
175176
with patch("torchx.schedulers.docker_scheduler.make_unique") as make_unique_ctx:
176177
make_unique_ctx.return_value = "app_name_42"
177-
info = self.scheduler._submit_dryrun(app, cfg)
178+
info = self.scheduler.submit_dryrun(app, cfg)
178179
self.assertEqual(
179180
info.request.containers[0].kwargs["environment"],
180181
{
@@ -190,7 +191,7 @@ def test_env(self) -> None:
190191
cfg = DockerOpts({"env": {"FOO_1": "BAR_1"}})
191192
with patch("torchx.schedulers.docker_scheduler.make_unique") as make_unique_ctx:
192193
make_unique_ctx.return_value = "app_name_42"
193-
info = self.scheduler._submit_dryrun(app, cfg)
194+
info = self.scheduler.submit_dryrun(app, cfg)
194195
self.assertEqual(
195196
info.request.containers[0].kwargs["environment"],
196197
{
@@ -200,13 +201,21 @@ def test_env(self) -> None:
200201
},
201202
)
202203

204+
def test_privileged(self) -> None:
205+
app = _test_app()
206+
cfg = DockerOpts({"privileged": True})
207+
with patch("torchx.schedulers.docker_scheduler.make_unique") as make_unique_ctx:
208+
make_unique_ctx.return_value = "app_name_42"
209+
info = self.scheduler.submit_dryrun(app, cfg)
210+
self.assertTrue(info.request.containers[0].kwargs["privileged"])
211+
203212
def test_long_hostname(self) -> None:
204213
app = _test_app()
205214
for role in app.roles:
206215
role.name = "ethology_explore_magic_calliope_divisive_whirl_dealt_lotus_oncology_facet_deerskin_blum_elective_spill_trammel_trainer"
207216
with patch("torchx.schedulers.docker_scheduler.make_unique") as make_unique_ctx:
208217
make_unique_ctx.return_value = "ethology_explore_magic_calliope_divisive_whirl_dealt_lotus_oncology_facet_deerskin_blum_elective_spill_trammel_12345"
209-
info = self.scheduler._submit_dryrun(app, DockerOpts())
218+
info = self.scheduler.submit_dryrun(app, DockerOpts())
210219
for container in info.request.containers:
211220
assert "name" in container.kwargs
212221
name = container.kwargs["name"]

0 commit comments

Comments
 (0)