Skip to content

Commit 7752996

Browse files
committed
Replace network_config with a dict of EndpointConfig
- Renamed parameter from `network_config` to `networking_config` to be more semantically correct with the rest of the API.
1 parent 0318ad8 commit 7752996

File tree

3 files changed

+159
-107
lines changed

3 files changed

+159
-107
lines changed

docker/models/containers.py

Lines changed: 19 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
import ntpath
33
from collections import namedtuple
44

5+
from .images import Image
6+
from .resource import Collection, Model
57
from ..api import APIClient
68
from ..constants import DEFAULT_DATA_CHUNK_SIZE
79
from ..errors import (
810
ContainerError, DockerException, ImageNotFound,
911
NotFound, create_unexpected_kwargs_error
1012
)
11-
from ..types import EndpointConfig, HostConfig, NetworkingConfig
13+
from ..types import HostConfig, NetworkingConfig
1214
from ..utils import version_gte
13-
from .images import Image
14-
from .resource import Collection, Model
1515

1616

1717
class Container(Model):
@@ -21,6 +21,7 @@ class Container(Model):
2121
query the Docker daemon for the current properties, causing
2222
:py:attr:`attrs` to be refreshed.
2323
"""
24+
2425
@property
2526
def name(self):
2627
"""
@@ -680,33 +681,13 @@ def run(self, image, command=None, stdout=True, stderr=False,
680681
This mode is incompatible with ``ports``.
681682
682683
Incompatible with ``network``.
683-
network_config (dict): A dictionary containing options that are
684-
passed to the network driver during the connection.
684+
networking_config (Dict[str, EndpointConfig]):
685+
Dictionary of EndpointConfig objects for each container network.
686+
The key is the name of the network.
685687
Defaults to ``None``.
686-
The dictionary contains the following keys:
687-
688-
- ``aliases`` (:py:class:`list`): A list of aliases for
689-
the network endpoint.
690-
Names in that list can be used within the network to
691-
reach this container. Defaults to ``None``.
692-
- ``links`` (:py:class:`list`): A list of links for
693-
the network endpoint endpoint.
694-
Containers declared in this list will be linked to this
695-
container. Defaults to ``None``.
696-
- ``ipv4_address`` (str): The IP address to assign to
697-
this container on the network, using the IPv4 protocol.
698-
Defaults to ``None``.
699-
- ``ipv6_address`` (str): The IP address to assign to
700-
this container on the network, using the IPv6 protocol.
701-
Defaults to ``None``.
702-
- ``link_local_ips`` (:py:class:`list`): A list of link-local
703-
(IPv4/IPv6) addresses.
704-
- ``driver_opt`` (dict): A dictionary of options to provide to
705-
the network driver. Defaults to ``None``.
706-
- ``mac_address`` (str): MAC Address to assign to the network
707-
interface. Defaults to ``None``. Requires API >= 1.25.
708688
709689
Used in conjuction with ``network``.
690+
710691
Incompatible with ``network_mode``.
711692
oom_kill_disable (bool): Whether to disable OOM killer.
712693
oom_score_adj (int): An integer value containing the score given
@@ -872,9 +853,9 @@ def run(self, image, command=None, stdout=True, stderr=False,
872853
'together.'
873854
)
874855

875-
if kwargs.get('network_config') and not kwargs.get('network'):
856+
if kwargs.get('networking_config') and not kwargs.get('network'):
876857
raise RuntimeError(
877-
'The option "network_config" can not be used '
858+
'The option "networking_config" can not be used '
878859
'without "network".'
879860
)
880861

@@ -1030,6 +1011,7 @@ def list(self, all=False, before=None, filters=None, limit=-1, since=None,
10301011

10311012
def prune(self, filters=None):
10321013
return self.client.api.prune_containers(filters=filters)
1014+
10331015
prune.__doc__ = APIClient.prune_containers.__doc__
10341016

10351017

@@ -1124,17 +1106,6 @@ def prune(self, filters=None):
11241106
]
11251107

11261108

1127-
NETWORKING_CONFIG_ARGS = [
1128-
'aliases',
1129-
'links',
1130-
'ipv4_address',
1131-
'ipv6_address',
1132-
'link_local_ips',
1133-
'driver_opt',
1134-
'mac_address'
1135-
]
1136-
1137-
11381109
def _create_container_args(kwargs):
11391110
"""
11401111
Convert arguments to create() to arguments to create_container().
@@ -1159,24 +1130,17 @@ def _create_container_args(kwargs):
11591130
host_config_kwargs['binds'] = volumes
11601131

11611132
network = kwargs.pop('network', None)
1162-
network_config = kwargs.pop('network_config', None)
1133+
networking_config = kwargs.pop('networking_config', None)
11631134
if network:
1164-
endpoint_config = None
1165-
1166-
if network_config:
1167-
clean_endpoint_args = {}
1168-
for arg_name in NETWORKING_CONFIG_ARGS:
1169-
if arg_name in network_config:
1170-
clean_endpoint_args[arg_name] = network_config[arg_name]
1171-
1172-
if clean_endpoint_args:
1173-
endpoint_config = EndpointConfig(
1174-
host_config_kwargs['version'], **clean_endpoint_args
1175-
)
1135+
if networking_config:
1136+
# Sanity check: check if the network is defined in the
1137+
# networking config dict, otherwise switch to None
1138+
if network not in networking_config:
1139+
networking_config = None
11761140

11771141
create_kwargs['networking_config'] = NetworkingConfig(
1178-
{network: endpoint_config}
1179-
) if endpoint_config else {network: None}
1142+
networking_config
1143+
) if networking_config else {network: None}
11801144
host_config_kwargs['network_mode'] = network
11811145

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

tests/integration/models_containers_test.py

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import pytest
66

77
import docker
8-
from ..helpers import random_name
9-
from ..helpers import requires_api_version
108
from .base import BaseIntegrationTest
119
from .base import TEST_API_VERSION
10+
from ..helpers import random_name
11+
from ..helpers import requires_api_version
1212

1313

1414
class ContainerCollectionTest(BaseIntegrationTest):
@@ -104,7 +104,7 @@ def test_run_with_network(self):
104104
assert 'Networks' in attrs['NetworkSettings']
105105
assert list(attrs['NetworkSettings']['Networks'].keys()) == [net_name]
106106

107-
def test_run_with_network_config(self):
107+
def test_run_with_networking_config(self):
108108
net_name = random_name()
109109
client = docker.from_env(version=TEST_API_VERSION)
110110
client.networks.create(net_name)
@@ -113,10 +113,16 @@ def test_run_with_network_config(self):
113113
test_aliases = ['hello']
114114
test_driver_opt = {'key1': 'a'}
115115

116+
networking_config = {
117+
net_name: client.api.create_endpoint_config(
118+
aliases=test_aliases,
119+
driver_opt=test_driver_opt
120+
)
121+
}
122+
116123
container = client.containers.run(
117124
'alpine', 'echo hello world', network=net_name,
118-
network_config={'aliases': test_aliases,
119-
'driver_opt': test_driver_opt},
125+
networking_config=networking_config,
120126
detach=True
121127
)
122128
self.tmp_containers.append(container.id)
@@ -131,7 +137,7 @@ def test_run_with_network_config(self):
131137
assert attrs['NetworkSettings']['Networks'][net_name]['DriverOpts'] \
132138
== test_driver_opt
133139

134-
def test_run_with_network_config_undeclared_params(self):
140+
def test_run_with_networking_config_with_undeclared_network(self):
135141
net_name = random_name()
136142
client = docker.from_env(version=TEST_API_VERSION)
137143
client.networks.create(net_name)
@@ -140,11 +146,41 @@ def test_run_with_network_config_undeclared_params(self):
140146
test_aliases = ['hello']
141147
test_driver_opt = {'key1': 'a'}
142148

149+
networking_config = {
150+
net_name: client.api.create_endpoint_config(
151+
aliases=test_aliases,
152+
driver_opt=test_driver_opt
153+
),
154+
'bar': client.api.create_endpoint_config(
155+
aliases=['test'],
156+
driver_opt={'key2': 'b'}
157+
),
158+
}
159+
160+
with pytest.raises(docker.errors.APIError) as e:
161+
container = client.containers.run(
162+
'alpine', 'echo hello world', network=net_name,
163+
networking_config=networking_config,
164+
detach=True
165+
)
166+
self.tmp_containers.append(container.id)
167+
168+
def test_run_with_networking_config_only_undeclared_network(self):
169+
net_name = random_name()
170+
client = docker.from_env(version=TEST_API_VERSION)
171+
client.networks.create(net_name)
172+
self.tmp_networks.append(net_name)
173+
174+
networking_config = {
175+
'bar': client.api.create_endpoint_config(
176+
aliases=['hello'],
177+
driver_opt={'key1': 'a'}
178+
),
179+
}
180+
143181
container = client.containers.run(
144182
'alpine', 'echo hello world', network=net_name,
145-
network_config={'aliases': test_aliases,
146-
'driver_opt': test_driver_opt,
147-
'undeclared_param': 'random_value'},
183+
networking_config=networking_config,
148184
detach=True
149185
)
150186
self.tmp_containers.append(container.id)
@@ -154,12 +190,9 @@ def test_run_with_network_config_undeclared_params(self):
154190
assert 'NetworkSettings' in attrs
155191
assert 'Networks' in attrs['NetworkSettings']
156192
assert list(attrs['NetworkSettings']['Networks'].keys()) == [net_name]
157-
assert attrs['NetworkSettings']['Networks'][net_name]['Aliases'] == \
158-
test_aliases
159-
assert attrs['NetworkSettings']['Networks'][net_name]['DriverOpts'] \
160-
== test_driver_opt
161-
assert 'undeclared_param' not in \
162-
attrs['NetworkSettings']['Networks'][net_name]
193+
assert attrs['NetworkSettings']['Networks'][net_name]['Aliases'] is None
194+
assert (attrs['NetworkSettings']['Networks'][net_name]['DriverOpts']
195+
is None)
163196

164197
def test_run_with_none_driver(self):
165198
client = docker.from_env(version=TEST_API_VERSION)
@@ -244,7 +277,7 @@ def test_get(self):
244277
container = client.containers.run("alpine", "sleep 300", detach=True)
245278
self.tmp_containers.append(container.id)
246279
assert client.containers.get(container.id).attrs[
247-
'Config']['Image'] == "alpine"
280+
'Config']['Image'] == "alpine"
248281

249282
def test_list(self):
250283
client = docker.from_env(version=TEST_API_VERSION)

0 commit comments

Comments
 (0)