diff --git a/docker/utils/utils.py b/docker/utils/utils.py index f36a3afb89..72d308f00d 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -290,14 +290,23 @@ def parse_host(addr, is_win32=False, tls=False): netloc = parsed_url.netloc if proto in ('tcp', 'ssh'): - port = parsed_url.port or 0 - if port <= 0: + try: + port = parsed_url.port + except ValueError as e: + raise errors.DockerException( + f'Invalid bind address format: {addr}' + ) from e + if port is None: if proto != 'ssh': raise errors.DockerException( f'Invalid bind address format: port is required: {addr}' ) port = 22 netloc = f'{parsed_url.netloc}:{port}' + elif port <= 0: + raise errors.DockerException( + f'Invalid bind address format: port is required: {addr}' + ) if not parsed_url.hostname: netloc = f'{DEFAULT_HTTP_HOST}:{port}' diff --git a/tests/unit/utils_test.py b/tests/unit/utils_test.py index 21da0b58e8..fccc2e10cc 100644 --- a/tests/unit/utils_test.py +++ b/tests/unit/utils_test.py @@ -285,6 +285,10 @@ def test_parse_host(self): 'unix:///sock/path#fragment', 'https://netloc:3333/path;params', 'ssh://:clearpassword@host:22', + 'tcp://host:0', + 'ssh://host:0', + 'tcp://host:99999', + 'tcp://host:notaport', ] valid_hosts = {