GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add `network_driver_opt` to client.containers run and create by Skazza94 · Pull Request #3083 · docker/docker-py

Add network_driver_opt to client.containers run and create #3083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
milas merged 2 commits into docker:master from Skazza94:master
Jan 27, 2023

Uh oh!

There was an error while loading. Please reload this page.

Merged

Add network_driver_opt to client.containers run and create #3083

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
c5dc2c8
Add `network_driver_opt` to client.containers run and create (#2896)
Skazza94 Dec 27, 2022
c647831
Fix docs
Skazza94 Dec 27, 2022
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

16 changes: 15 additions & 1 deletion 16 docker/models/containers.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,10 @@ def run(self, image, command=None, stdout=True, stderr=False,
This mode is incompatible with ``ports``.

Incompatible with ``network``.
network_driver_opt (dict): A dictionary of options to provide
to the network driver. Defaults to ``None``. Used in
conjuction with ``network``. Incompatible
with ``network_mode``.
oom_kill_disable (bool): Whether to disable OOM killer.
oom_score_adj (int): An integer value containing the score given
to the container in order to tune OOM killer preferences.
Expand Down Expand Up @@ -842,6 +846,12 @@ def run(self, image, command=None, stdout=True, stderr=False,
'together.'
)

if kwargs.get('network_driver_opt') and not kwargs.get('network'):
raise RuntimeError(
'The options "network_driver_opt" can not be used '
'without "network".'
)

try:
container = self.create(image=image, command=command,
detach=detach, **kwargs)
Expand Down Expand Up @@ -1112,8 +1122,12 @@ def _create_container_args(kwargs):
host_config_kwargs['binds'] = volumes

network = kwargs.pop('network', None)
network_driver_opt = kwargs.pop('network_driver_opt', None)
if network:
create_kwargs['networking_config'] = {network: None}
network_configuration = {'driver_opt': network_driver_opt} \
if network_driver_opt else None

create_kwargs['networking_config'] = {network: network_configuration}
host_config_kwargs['network_mode'] = network

# All kwargs should have been consumed by this point, so raise
Expand Down
84 changes: 83 additions & 1 deletion 84 tests/unit/models_containers_test.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def test_create_container_args(self):
name='somename',
network_disabled=False,
network='foo',
network_driver_opt={'key1': 'a'},
oom_kill_disable=True,
oom_score_adj=5,
pid_mode='host',
Expand Down Expand Up @@ -188,7 +189,7 @@ def test_create_container_args(self):
mac_address='abc123',
name='somename',
network_disabled=False,
networking_config={'foo': None},
networking_config={'foo': {'driver_opt': {'key1': 'a'}}},
platform='linux',
ports=[('1111', 'tcp'), ('2222', 'tcp')],
stdin_open=True,
Expand Down Expand Up @@ -345,6 +346,42 @@ def test_run_platform(self):
host_config={'NetworkMode': 'default'},
)

def test_run_network_driver_opts_without_network(self):
client = make_fake_client()

with pytest.raises(RuntimeError):
client.containers.run(
image='alpine',
network_driver_opt={'key1': 'a'}
)

def test_run_network_driver_opts_with_network_mode(self):
client = make_fake_client()

with pytest.raises(RuntimeError):
client.containers.run(
image='alpine',
network_mode='none',
network_driver_opt={'key1': 'a'}
)

def test_run_network_driver_opts(self):
client = make_fake_client()

client.containers.run(
image='alpine',
network='foo',
network_driver_opt={'key1': 'a'}
)

client.api.create_container.assert_called_with(
detach=False,
image='alpine',
command=None,
networking_config={'foo': {'driver_opt': {'key1': 'a'}}},
host_config={'NetworkMode': 'foo'}
)

def test_create(self):
client = make_fake_client()
container = client.containers.create(
Expand Down Expand Up @@ -372,6 +409,51 @@ def test_create_with_image_object(self):
host_config={'NetworkMode': 'default'}
)

def test_create_network_driver_opts_without_network(self):
client = make_fake_client()

client.containers.create(
image='alpine',
network_driver_opt={'key1': 'a'}
)

client.api.create_container.assert_called_with(
image='alpine',
command=None,
host_config={'NetworkMode': 'default'}
)

def test_create_network_driver_opts_with_network_mode(self):
client = make_fake_client()

client.containers.create(
image='alpine',
network_mode='none',
network_driver_opt={'key1': 'a'}
)

client.api.create_container.assert_called_with(
image='alpine',
command=None,
host_config={'NetworkMode': 'none'}
)

def test_create_network_driver_opts(self):
client = make_fake_client()

client.containers.create(
image='alpine',
network='foo',
network_driver_opt={'key1': 'a'}
)

client.api.create_container.assert_called_with(
image='alpine',
command=None,
networking_config={'foo': {'driver_opt': {'key1': 'a'}}},
host_config={'NetworkMode': 'foo'}
)

def test_get(self):
client = make_fake_client()
container = client.containers.get(FAKE_CONTAINER_ID)
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.