Skip to content

Commit cf68ebc

Browse files
authored
Merge pull request #1604 from docker/1433-run-networks
Replace erroneous networks argument in containers.run
2 parents 007ab67 + 95297dc commit cf68ebc

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed

docker/models/containers.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def exec_run(self, cmd, stdout=True, stderr=True, stdin=False, tty=False,
147147
148148
Returns:
149149
(generator or str): If ``stream=True``, a generator yielding
150-
response chunks. A string containing response data otherwise.
150+
response chunks. A string containing response data otherwise.
151151
152152
Raises:
153153
:py:class:`docker.errors.APIError`
@@ -546,10 +546,12 @@ def run(self, image, command=None, stdout=True, stderr=False,
546546
behavior. Accepts number between 0 and 100.
547547
memswap_limit (str or int): Maximum amount of memory + swap a
548548
container is allowed to consume.
549-
networks (:py:class:`list`): A list of network names to connect
550-
this container to.
551549
name (str): The name for this container.
552550
nano_cpus (int): CPU quota in units of 10-9 CPUs.
551+
network (str): Name of the network this container will be connected
552+
to at creation time. You can connect to additional networks
553+
using :py:meth:`Network.connect`. Incompatible with
554+
``network_mode``.
553555
network_disabled (bool): Disable networking.
554556
network_mode (str): One of:
555557
@@ -559,6 +561,7 @@ def run(self, image, command=None, stdout=True, stderr=False,
559561
- ``container:<name|id>`` Reuse another container's network
560562
stack.
561563
- ``host`` Use the host network stack.
564+
Incompatible with ``network``.
562565
oom_kill_disable (bool): Whether to disable OOM killer.
563566
oom_score_adj (int): An integer value containing the score given
564567
to the container in order to tune OOM killer preferences.
@@ -680,6 +683,12 @@ def run(self, image, command=None, stdout=True, stderr=False,
680683
raise RuntimeError("The options 'detach' and 'remove' cannot be "
681684
"used together.")
682685

686+
if kwargs.get('network') and kwargs.get('network_mode'):
687+
raise RuntimeError(
688+
'The options "network" and "network_mode" can not be used '
689+
'together.'
690+
)
691+
683692
try:
684693
container = self.create(image=image, command=command,
685694
detach=detach, **kwargs)
@@ -902,10 +911,10 @@ def _create_container_args(kwargs):
902911
if volumes:
903912
host_config_kwargs['binds'] = volumes
904913

905-
networks = kwargs.pop('networks', [])
906-
if networks:
907-
create_kwargs['networking_config'] = {network: None
908-
for network in networks}
914+
network = kwargs.pop('network', None)
915+
if network:
916+
create_kwargs['networking_config'] = {network: None}
917+
host_config_kwargs['network_mode'] = network
909918

910919
# All kwargs should have been consumed by this point, so raise
911920
# error if any are left

docker/models/plugins.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ def upgrade(self, remote=None):
103103
104104
Args:
105105
remote (string): Remote reference to upgrade to. The
106-
``:latest`` tag is optional and is the default if omitted.
107-
Default: this plugin's name.
106+
``:latest`` tag is optional and is the default if omitted.
107+
Default: this plugin's name.
108108
109109
Returns:
110110
A generator streaming the decoded API logs

docker/types/services.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def __init__(self, image, command=None, args=None, hostname=None, env=None,
127127
class Mount(dict):
128128
"""
129129
Describes a mounted folder's configuration inside a container. A list of
130-
:py:class:`Mount`s would be used as part of a
130+
:py:class:`Mount` would be used as part of a
131131
:py:class:`~docker.types.ContainerSpec`.
132132
133133
Args:

tests/integration/models_containers_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import docker
22
import tempfile
33
from .base import BaseIntegrationTest, TEST_API_VERSION
4+
from ..helpers import random_name
45

56

67
class ContainerCollectionTest(BaseIntegrationTest):
@@ -69,6 +70,24 @@ def test_run_with_named_volume(self):
6970
)
7071
self.assertEqual(out, b'hello\n')
7172

73+
def test_run_with_network(self):
74+
net_name = random_name()
75+
client = docker.from_env(version=TEST_API_VERSION)
76+
client.networks.create(net_name)
77+
self.tmp_networks.append(net_name)
78+
79+
container = client.containers.run(
80+
'alpine', 'echo hello world', network=net_name,
81+
detach=True
82+
)
83+
self.tmp_containers.append(container.id)
84+
85+
attrs = container.attrs
86+
87+
assert 'NetworkSettings' in attrs
88+
assert 'Networks' in attrs['NetworkSettings']
89+
assert list(attrs['NetworkSettings']['Networks'].keys()) == [net_name]
90+
7291
def test_get(self):
7392
client = docker.from_env(version=TEST_API_VERSION)
7493
container = client.containers.run("alpine", "sleep 300", detach=True)

tests/unit/models_containers_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ def test_create_container_args(self):
7171
memswap_limit=456,
7272
name='somename',
7373
network_disabled=False,
74-
network_mode='blah',
75-
networks=['foo'],
74+
network='foo',
7675
oom_kill_disable=True,
7776
oom_score_adj=5,
7877
pid_mode='host',
@@ -153,7 +152,7 @@ def test_create_container_args(self):
153152
'MemoryReservation': 123,
154153
'MemorySwap': 456,
155154
'MemorySwappiness': 2,
156-
'NetworkMode': 'blah',
155+
'NetworkMode': 'foo',
157156
'OomKillDisable': True,
158157
'OomScoreAdj': 5,
159158
'PidMode': 'host',

0 commit comments

Comments
 (0)