mirror of
https://github.com/docker/compose.git
synced 2025-07-20 20:24:30 +02:00
Merge pull request #1659 from viranch/hostip-random-port
Allow scaling services that have port binding of the form `host_ip::container_port`
This commit is contained in:
commit
5d60fbe31e
@ -764,10 +764,28 @@ class Service(object):
|
|||||||
return self.options.get('container_name')
|
return self.options.get('container_name')
|
||||||
|
|
||||||
def specifies_host_port(self):
|
def specifies_host_port(self):
|
||||||
for port in self.options.get('ports', []):
|
def has_host_port(binding):
|
||||||
if ':' in str(port):
|
_, external_bindings = split_port(binding)
|
||||||
|
|
||||||
|
# there are no external bindings
|
||||||
|
if external_bindings is None:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# we only need to check the first binding from the range
|
||||||
|
external_binding = external_bindings[0]
|
||||||
|
|
||||||
|
# non-tuple binding means there is a host port specified
|
||||||
|
if not isinstance(external_binding, tuple):
|
||||||
return True
|
return True
|
||||||
return False
|
|
||||||
|
# extract actual host port from tuple of (host_ip, host_port)
|
||||||
|
_, host_port = external_binding
|
||||||
|
if host_port is not None:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
return any(has_host_port(binding) for binding in self.options.get('ports', []))
|
||||||
|
|
||||||
def pull(self, ignore_pull_failures=False):
|
def pull(self, ignore_pull_failures=False):
|
||||||
if 'image' not in self.options:
|
if 'image' not in self.options:
|
||||||
|
@ -437,6 +437,68 @@ class ServiceTest(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
self.assertEqual(config_dict, expected)
|
self.assertEqual(config_dict, expected)
|
||||||
|
|
||||||
|
def test_specifies_host_port_with_no_ports(self):
|
||||||
|
service = Service(
|
||||||
|
'foo',
|
||||||
|
image='foo')
|
||||||
|
self.assertEqual(service.specifies_host_port(), False)
|
||||||
|
|
||||||
|
def test_specifies_host_port_with_container_port(self):
|
||||||
|
service = Service(
|
||||||
|
'foo',
|
||||||
|
image='foo',
|
||||||
|
ports=["2000"])
|
||||||
|
self.assertEqual(service.specifies_host_port(), False)
|
||||||
|
|
||||||
|
def test_specifies_host_port_with_host_port(self):
|
||||||
|
service = Service(
|
||||||
|
'foo',
|
||||||
|
image='foo',
|
||||||
|
ports=["1000:2000"])
|
||||||
|
self.assertEqual(service.specifies_host_port(), True)
|
||||||
|
|
||||||
|
def test_specifies_host_port_with_host_ip_no_port(self):
|
||||||
|
service = Service(
|
||||||
|
'foo',
|
||||||
|
image='foo',
|
||||||
|
ports=["127.0.0.1::2000"])
|
||||||
|
self.assertEqual(service.specifies_host_port(), False)
|
||||||
|
|
||||||
|
def test_specifies_host_port_with_host_ip_and_port(self):
|
||||||
|
service = Service(
|
||||||
|
'foo',
|
||||||
|
image='foo',
|
||||||
|
ports=["127.0.0.1:1000:2000"])
|
||||||
|
self.assertEqual(service.specifies_host_port(), True)
|
||||||
|
|
||||||
|
def test_specifies_host_port_with_container_port_range(self):
|
||||||
|
service = Service(
|
||||||
|
'foo',
|
||||||
|
image='foo',
|
||||||
|
ports=["2000-3000"])
|
||||||
|
self.assertEqual(service.specifies_host_port(), False)
|
||||||
|
|
||||||
|
def test_specifies_host_port_with_host_port_range(self):
|
||||||
|
service = Service(
|
||||||
|
'foo',
|
||||||
|
image='foo',
|
||||||
|
ports=["1000-2000:2000-3000"])
|
||||||
|
self.assertEqual(service.specifies_host_port(), True)
|
||||||
|
|
||||||
|
def test_specifies_host_port_with_host_ip_no_port_range(self):
|
||||||
|
service = Service(
|
||||||
|
'foo',
|
||||||
|
image='foo',
|
||||||
|
ports=["127.0.0.1::2000-3000"])
|
||||||
|
self.assertEqual(service.specifies_host_port(), False)
|
||||||
|
|
||||||
|
def test_specifies_host_port_with_host_ip_and_port_range(self):
|
||||||
|
service = Service(
|
||||||
|
'foo',
|
||||||
|
image='foo',
|
||||||
|
ports=["127.0.0.1:1000-2000:2000-3000"])
|
||||||
|
self.assertEqual(service.specifies_host_port(), True)
|
||||||
|
|
||||||
|
|
||||||
class NetTestCase(unittest.TestCase):
|
class NetTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user