8000 client: add `network_driver_opt` to container run and create (#3083) · docker/docker-py@ee9151f · GitHub
[go: up one dir, main page]

Skip to content

Commit ee9151f

Browse files
authored
client: add network_driver_opt to container run and create (#3083)
Signed-off-by: Mariano Scazzariello <marianoscazzariello@gmail.com>
1 parent 2494d63 commit ee9151f

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
@@ -678,6 +678,10 @@ def run(self, image, command=None, stdout=True, stderr=False,
678678
This mode is incompatible with ``ports``.
679679
680680
Incompatible with ``network``.
681+
network_driver_opt (dict): A dictionary of options to provide
682+
to the network driver. Defaults to ``None``. Used in
683+
conjuction with ``network``. Incompatible
684+
with ``network_mode``.
681685
oom_kill_disable (bool): Whether to disable OOM killer.
682686
oom_score_adj (int): An integer value containing the score given
683687
to the container in order to tune OOM killer preferences.
@@ -842,6 +846,12 @@ def run(self, image, command=None, stdout=True, stderr=False,
842846
'together.'
843847
)
844848

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

11141124
network = kwargs.pop('network', None)
1125+
network_driver_opt = kwargs.pop('network_driver_opt', None)
11151126
if network:
1116-
create_kwargs['networking_config'] = {network: None}
1127+
network_configuration = {'driver_opt': network_driver_opt} \
1128+
if network_driver_opt else None
1129+
1130+
create_kwargs['networking_config'] = {network: network_configuration}
11171131
host_config_kwargs['network_mode'] = network
11181132

11191133
# 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+ CDD2
)
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)
0