Skip to content

Commit 6da140e

Browse files
authored
Merge pull request #2511 from daeseokyoun/handle-network-host
raise an error for binding specific ports in 'host' mode of network
2 parents bb1c528 + 433264d commit 6da140e

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

docker/api/container.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,8 @@ def create_host_config(self, *args, **kwargs):
523523
- ``container:<name|id>`` Reuse another container's network
524524
stack.
525525
- ``host`` Use the host network stack.
526+
This mode is incompatible with ``port_bindings``.
527+
526528
oom_kill_disable (bool): Whether to disable OOM killer.
527529
oom_score_adj (int): An integer value containing the score given
528530
to the container in order to tune OOM killer preferences.
@@ -531,7 +533,8 @@ def create_host_config(self, *args, **kwargs):
531533
pids_limit (int): Tune a container's pids limit. Set ``-1`` for
532534
unlimited.
533535
port_bindings (dict): See :py:meth:`create_container`
534-
for more information.
536+
for more information.
537+
Imcompatible with ``host`` in ``network_mode``.
535538
privileged (bool): Give extended privileges to this container.
536539
publish_all_ports (bool): Publish all ports to the host.
537540
read_only (bool): Mount the container's root filesystem as read

docker/models/containers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ def run(self, image, command=None, stdout=True, stderr=False,
649649
- ``container:<name|id>`` Reuse another container's network
650650
stack.
651651
- ``host`` Use the host network stack.
652+
This mode is incompatible with ``ports``.
652653
653654
Incompatible with ``network``.
654655
oom_kill_disable (bool): Whether to disable OOM killer.
@@ -682,6 +683,7 @@ def run(self, image, command=None, stdout=True, stderr=False,
682683
to a single container port. For example,
683684
``{'1111/tcp': [1234, 4567]}``.
684685
686+
Imcompatible with ``host`` in ``network_mode``.
685687
privileged (bool): Give extended privileges to this container.
686688
publish_all_ports (bool): Publish all ports to the host.
687689
read_only (bool): Mount the container's root filesystem as read

docker/types/containers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,11 @@ def __init__(self, version, binds=None, port_bindings=None,
334334
if dns_search:
335335
self['DnsSearch'] = dns_search
336336

337+
if network_mode is 'host' and port_bindings is not None:
338+
raise host_config_incompatible_error(
339+
'network_mode', 'host', 'port_bindings'
340+
)
341+
337342
if network_mode:
338343
self['NetworkMode'] = network_mode
339344
elif network_mode is None:
@@ -664,6 +669,13 @@ def host_config_value_error(param, param_value):
664669
return ValueError(error_msg.format(param, param_value))
665670

666671

672+
def host_config_incompatible_error(param, param_value, incompatible_param):
673+
error_msg = 'Incompatible {1} in {0} is not compatible with {2}'
674+
return errors.InvalidArgument(
675+
error_msg.format(param, param_value, incompatible_param)
676+
)
677+
678+
667679
class ContainerConfig(dict):
668680
def __init__(
669681
self, version, image, command, hostname=None, user=None, detach=False,

0 commit comments

Comments
 (0)