diff options
author | Daeseok Youn <daeseok.youn@navercorp.com> | 2020-02-07 18:11:00 +0900 |
---|---|---|
committer | aiordache <anca.iordache@docker.com> | 2021-02-15 15:44:29 +0100 |
commit | 3a8565029b287138a231f60d2d26be8f71381c8c (patch) | |
tree | f60d67c3bf01277d08984edbc821b5f7e47e0bbb | |
parent | 817023f9edc34dfe6d056c1ee649ec66ba86533e (diff) | |
download | docker-py-3a8565029b287138a231f60d2d26be8f71381c8c.tar.gz |
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 <daeseok.youn@navercorp.com>
-rw-r--r-- | docker/api/container.py | 7 | ||||
-rw-r--r-- | docker/models/containers.py | 5 | ||||
-rw-r--r-- | docker/types/containers.py | 12 |
3 files changed, 23 insertions, 1 deletions
diff --git a/docker/api/container.py b/docker/api/container.py index 24eb9c1..cfd5147 100644 --- a/docker/api/container.py +++ b/docker/api/container.py @@ -523,6 +523,10 @@ class ContainerApiMixin(object): - ``container:<name|id>`` Reuse another container's network stack. - ``host`` Use the host network stack. + This mode is incompatible with ``port_bindings``. + If ``host`` is used as network_mode, all of listed up to + ``port_bindings``` are ignored in running container. + 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. @@ -531,7 +535,8 @@ class ContainerApiMixin(object): pids_limit (int): Tune a container's pids limit. Set ``-1`` for unlimited. port_bindings (dict): See :py:meth:`create_container` - for more information. + for more information. The binding ports are ignored in + ``host`` as network mode. privileged (bool): Give extended privileges to this container. publish_all_ports (bool): Publish all ports to the host. read_only (bool): Mount the container's root filesystem as read diff --git a/docker/models/containers.py b/docker/models/containers.py index 0c2b855..bcd7801 100644 --- a/docker/models/containers.py +++ b/docker/models/containers.py @@ -649,6 +649,9 @@ class ContainerCollection(Collection): - ``container:<name|id>`` Reuse another container's network stack. - ``host`` Use the host network stack. + This mode is incompatible with ``ports``. If ``host`` is + used as network_mode, all of listed up to ``ports``` are + ignored in running container. Incompatible with ``network``. oom_kill_disable (bool): Whether to disable OOM killer. @@ -667,6 +670,8 @@ class ContainerCollection(Collection): ``port/protocol``, where the protocol is either ``tcp``, ``udp``, or ``sctp``. + Ports are ignored to bind with ``host`` as network mode. + The values of the dictionary are the corresponding ports to open on the host, which can be either: diff --git a/docker/types/containers.py b/docker/types/containers.py index 44bfcfd..1df3fff 100644 --- a/docker/types/containers.py +++ b/docker/types/containers.py @@ -334,6 +334,11 @@ class HostConfig(dict): if dns_search: self['DnsSearch'] = dns_search + if network_mode is 'host' and port_bindings is not None: + raise host_config_incompatible_error( + 'network_mode', 'host', 'port_bindings' + ) + if network_mode: self['NetworkMode'] = network_mode elif network_mode is None: @@ -664,6 +669,13 @@ def host_config_value_error(param, param_value): return ValueError(error_msg.format(param, param_value)) +def host_config_incompatible_error(param, param_value, incompatible_param): + error_msg = 'Incompatible {1} in {0} is not compatible with {2}' + return errors.InvalidArgument( + error_msg.format(param, param_value, incompatible_param) + ) + + class ContainerConfig(dict): def __init__( self, version, image, command, hostname=None, user=None, detach=False, |