Skip to content

Commit 3585f5d

Browse files
YinghongLiuYinghong Liu
andauthored
Fix local docker invalid container name created by trimming (#958)
Co-authored-by: Yinghong Liu <[email protected]>
1 parent 648913c commit 3585f5d

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

torchx/schedulers/docker_scheduler.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import fnmatch
1010
import logging
1111
import os.path
12+
import re
1213
import tempfile
1314
from dataclasses import dataclass
1415
from datetime import datetime
@@ -265,9 +266,12 @@ def _submit_dryrun(self, app: AppDef, cfg: DockerOpts) -> AppDryRunInfo[DockerJo
265266
rank0_env="TORCHX_RANK0_HOST",
266267
)
267268
replica_role = values.apply(role)
268-
# trim app_id and role name in case name is longer than 64 letters. Assume replica_id is less than 10_000.
269-
name = f"{app_id[-30:]}-{role.name[:30]}-{replica_id}"
270-
269+
# trim app_id and role name in case name is longer than 64 letters. Assume replica_id is less than 10_000. Remove invalid prefixes (https://github.com/moby/moby/blob/master/daemon/names/names.go#L6).
270+
name = re.sub(
271+
r"^[^a-zA-Z0-9]+",
272+
"",
273+
f"{app_id[-30:]}-{role.name[:30]}-{replica_id}",
274+
)
271275
env = default_env.copy()
272276
if replica_role.env:
273277
env.update(replica_role.env)

torchx/schedulers/test/docker_scheduler_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,15 @@ def test_long_hostname(self) -> None:
221221
for role in app.roles:
222222
role.name = "ethology_explore_magic_calliope_divisive_whirl_dealt_lotus_oncology_facet_deerskin_blum_elective_spill_trammel_trainer"
223223
with patch("torchx.schedulers.docker_scheduler.make_unique") as make_unique_ctx:
224-
make_unique_ctx.return_value = "ethology_explore_magic_calliope_divisive_whirl_dealt_lotus_oncology_facet_deerskin_blum_elective_spill_trammel_12345"
224+
make_unique_ctx.return_value = "ethology_explore_magic_calliope_divisive_whirl_dealt_lotus_oncology_facet_deerskin__.-_elective_spill_trammel_1234"
225225
info = self.scheduler.submit_dryrun(app, DockerOpts())
226226
for container in info.request.containers:
227227
assert "name" in container.kwargs
228228
name = container.kwargs["name"]
229229
assert isinstance(name, str)
230230
assert len(name) < 65
231+
# Assert match container name rules https://github.com/moby/moby/blob/master/daemon/names/names.go#L6
232+
self.assertRegex(name, r"[a-zA-Z0-9][a-zA-Z0-9_.-]")
231233

232234

233235
if has_docker():

0 commit comments

Comments
 (0)