Skip to content

Commit 9c53024

Browse files
author
Daeseok Youn
committed
raise an error for binding specific ports in 'host' mode of network
The binding ports are ignored where the network mode is 'host'. It could be a problem in case of using these options together on Mac or Windows OS. Because the limitation that could not use the 'host' in network_mode on Mac and Windows. When 'host' mode is set on network_mode, the specific ports in 'ports' are ignored so the network is not able to be accessed through defined ports by developer. Signed-off-by: Daeseok Youn <[email protected]>
1 parent 8002222 commit 9c53024

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

docker/api/container.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,10 @@ 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+
If ``host`` is used as network_mode, all of listed up to
528+
``port_bindings``` are ignored in running container.
529+
526530
oom_kill_disable (bool): Whether to disable OOM killer.
527531
oom_score_adj (int): An integer value containing the score given
528532
to the container in order to tune OOM killer preferences.
@@ -531,7 +535,8 @@ def create_host_config(self, *args, **kwargs):
531535
pids_limit (int): Tune a container's pids limit. Set ``-1`` for
532536
unlimited.
533537
port_bindings (dict): See :py:meth:`create_container`
534-
for more information.
538+
for more information. The binding ports are ignored in
539+
``host`` as network mode.
535540
privileged (bool): Give extended privileges to this container.
536541
publish_all_ports (bool): Publish all ports to the host.
537542
read_only (bool): Mount the container's root filesystem as read

docker/models/containers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,9 @@ 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``. If ``host`` is
653+
used as network_mode, all of listed up to ``ports``` are
654+
ignored in running container.
652655
653656
Incompatible with ``network``.
654657
oom_kill_disable (bool): Whether to disable OOM killer.
@@ -667,6 +670,8 @@ def run(self, image, command=None, stdout=True, stderr=False,
667670
``port/protocol``, where the protocol is either ``tcp``,
668671
``udp``, or ``sctp``.
669672
673+
Ports are ignored to bind with ``host`` as network mode.
674+
670675
The values of the dictionary are the corresponding ports to
671676
open on the host, which can be either:
672677

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)