Skip to content

Commit aca129d

Browse files
committed
Merge branch 'master'
(Old PR unintentionally went to the `master` branch.)
2 parents 34e6829 + ee9151f commit aca129d

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

docker/models/containers.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,10 @@ def run(self, image, command=None, stdout=True, stderr=False,
679679
This mode is incompatible with ``ports``.
680680
681681
Incompatible with ``network``.
682+
network_driver_opt (dict): A dictionary of options to provide
683+
to the network driver. Defaults to ``None``. Used in
684+
conjuction with ``network``. Incompatible
685+
with ``network_mode``.
682686
oom_kill_disable (bool): Whether to disable OOM killer.
683687
oom_score_adj (int): An integer value containing the score given
684688
to the container in order to tune OOM killer preferences.
@@ -843,6 +847,12 @@ def run(self, image, command=None, stdout=True, stderr=False,
843847
'together.'
844848
)
845849

850+
if kwargs.get('network_driver_opt') and not kwargs.get('network'):
851+
raise RuntimeError(
852+
'The options "network_driver_opt" can not be used '
853+
'without "network".'
854+
)
855+
846856
try:
847857
container = self.create(image=image, command=command,
848858
detach=detach, **kwargs)
@@ -1113,8 +1123,12 @@ def _create_container_args(kwargs):
11131123
host_config_kwargs['binds'] = volumes
11141124

11151125
network = kwargs.pop('network', None)
1126+
network_driver_opt = kwargs.pop('network_driver_opt', None)
11161127
if network:
1117-
create_kwargs['networking_config'] = {network: None}
1128+
network_configuration = {'driver_opt': network_driver_opt} \
1129+
if network_driver_opt else None
1130+
1131+
create_kwargs['networking_config'] = {network: network_configuration}
11181132
host_config_kwargs['network_mode'] = network
11191133

11201134
# All kwargs should have been consumed by this point, so raise

tests/unit/models_containers_test.py

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def test_create_container_args(self):
7474
name='somename',
7575
network_disabled=False,
7676
network='foo',
77+
network_driver_opt={'key1': 'a'},
7778
oom_kill_disable=True,
7879
oom_score_adj=5,
7980
pid_mode='host',
@@ -188,7 +189,7 @@ def test_create_container_args(self):
188189
mac_address='abc123',
189190
name='somename',
190191
network_disabled=False,
191-
networking_config={'foo': None},
192+
networking_config={'foo': {'driver_opt': {'key1': 'a'}}},
192193
platform='linux',
193194
ports=[('1111', 'tcp'), ('2222', 'tcp')],
194195
stdin_open=True,
@@ -345,6 +346,42 @@ def test_run_platform(self):
345346
host_config={'NetworkMode': 'default'},
346347
)
347348

349+
def test_run_network_driver_opts_without_network(self):
350+
client = make_fake_client()
351+
352+
with pytest.raises(RuntimeError):
353+
client.containers.run(
354+
image='alpine',
355+
network_driver_opt={'key1': 'a'}
356+
)
357+
358+
def test_run_network_driver_opts_with_network_mode(self):
359+
client = make_fake_client()
360+
361+
with pytest.raises(RuntimeError):
362+
client.containers.run(
363+
image='alpine',
364+
network_mode='none',
365+
network_driver_opt={'key1': 'a'}
366+
)
367+
368+
def test_run_network_driver_opts(self):
369+
client = make_fake_client()
370+
371+
client.containers.run(
372+
image='alpine',
373+
network='foo',
374+
network_driver_opt={'key1': 'a'}
375+
)
376+
377+
client.api.create_container.assert_called_with(
378+
detach=False,
379+
image='alpine',
380+
command=None,
381+
networking_config={'foo': {'driver_opt': {'key1': 'a'}}},
382+
host_config={'NetworkMode': 'foo'}
383+
)
384+
348385
def test_create(self):
349386
client = make_fake_client()
350387
container = client.containers.create(
@@ -372,6 +409,51 @@ def test_create_with_image_object(self):
372409
host_config={'NetworkMode': 'default'}
373410
)
374411

412+
def test_create_network_driver_opts_without_network(self):
413+
client = make_fake_client()
414+
415+
client.containers.create(
416+
image='alpine',
417+
network_driver_opt={'key1': 'a'}
418+
)
419+
420+
client.api.create_container.assert_called_with(
421+
image='alpine',
422+
command=None,
423+
host_config={'NetworkMode': 'default'}
424+
)
425+
426+
def test_create_network_driver_opts_with_network_mode(self):
427+
client = make_fake_client()
428+
429+
client.containers.create(
430+
image='alpine',
431+
network_mode='none',
432+
network_driver_opt={'key1': 'a'}
433+
)
434+
435+
client.api.create_container.assert_called_with(
436+
image='alpine',
437+
command=None,
438+
host_config={'NetworkMode': 'none'}
439+
)
440+
441+
def test_create_network_driver_opts(self):
442+
client = make_fake_client()
443+
444+
client.containers.create(
445+
image='alpine',
446+
network='foo',
447+
network_driver_opt={'key1': 'a'}
448+
)
449+
450+
client.api.create_container.assert_called_with(
451+
image='alpine',
452+
command=None,
453+
networking_config={'foo': {'driver_opt': {'key1': 'a'}}},
454+
host_config={'NetworkMode': 'foo'}
455+
)
456+
375457
def test_get(self):
376458
client = make_fake_client()
377459
container = client.containers.get(FAKE_CONTAINER_ID)

0 commit comments

Comments
 (0)