Skip to content

Commit 786be3e

Browse files
authored
Merge pull request #405 from girder/docker-run-options
Add the ability to always add additional docker run options
2 parents 775c1c5 + 507c11d commit 786be3e

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

girder_worker/docker/tasks/__init__.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime
2+
import json
23
import os
34
import shutil
45
import socket
@@ -33,7 +34,7 @@
3334
from girder_worker_utils import _walk_obj
3435

3536

36-
BLACKLISTED_DOCKER_RUN_ARGS = ['tty', 'detach']
37+
BLACKLISTED_DOCKER_RUN_ARGS = ['tty', 'detach', 'volumes']
3738

3839

3940
def _pull_image(image):
@@ -364,6 +365,28 @@ def _cleanup_temp_volumes(self, temp_volumes, default_temp_volume):
364365
shutil.rmtree(v.host_path)
365366

366367

368+
def _add_environment_kargs(run_kwargs):
369+
envkey = 'GIRDER_WORKER_DOCKER_RUN_OPTIONS'
370+
if envkey not in os.environ:
371+
return
372+
try:
373+
opts = json.loads(os.environ[envkey])
374+
extra_run_kwargs = {k: v for k, v in opts.items() if k not in BLACKLISTED_DOCKER_RUN_ARGS}
375+
run_kwargs.update(extra_run_kwargs)
376+
if 'volumes' in opts:
377+
if isinstance(opts['volumes'], list):
378+
opts['volumes'] = {
379+
v.split(':')[0]: {
380+
'bind': v.split(':')[1],
381+
'mode': (v + ':ro').split(':')[2]}
382+
for v in opts['volumes']}
383+
if 'volumes' not in run_kwargs:
384+
run_kwargs['volumes'] = {}
385+
run_kwargs['volumes'].update(opts['volumes'])
386+
except Exception:
387+
logger.exception(f'Failed to parse {envkey}')
388+
389+
367390
def _docker_run(task, image, pull_image=True, entrypoint=None, container_args=None,
368391
volumes=None, remove_container=True, stream_connectors=None, **kwargs):
369392
volumes = volumes or {}
@@ -382,6 +405,7 @@ def _docker_run(task, image, pull_image=True, entrypoint=None, container_args=No
382405
'volumes': volumes,
383406
'detach': True
384407
}
408+
_add_environment_kargs(run_kwargs)
385409

386410
# Allow run args to be overridden,filter out any we don't want to override
387411
extra_run_kwargs = {k: v for k, v in kwargs.items() if k not in BLACKLISTED_DOCKER_RUN_ARGS}

0 commit comments

Comments
 (0)