Skip to content

Commit 1362938

Browse files
committed
Default to 127.0.0.1:2375 on Windows
Following the logic of the Docker client. Signed-off-by: Aanand Prasad <[email protected]>
1 parent 33acb9d commit 1362938

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

docker/clientbase.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import struct
3+
import sys
34

45
import requests
56
import requests.exceptions
@@ -31,7 +32,7 @@ def __init__(self, base_url=None, version=None,
3132

3233
self._auth_configs = auth.load_config()
3334

34-
base_url = utils.parse_host(base_url)
35+
base_url = utils.parse_host(base_url, sys.platform)
3536
if base_url.startswith('http+unix://'):
3637
self._custom_adapter = unixconn.UnixAdapter(base_url, timeout)
3738
self.mount('http+docker://', self._custom_adapter)

docker/utils/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,15 @@ def parse_repository_tag(repo):
272272
# fd:// protocol unsupported (for obvious reasons)
273273
# Added support for http and https
274274
# Protocol translation: tcp -> http, unix -> http+unix
275-
def parse_host(addr):
275+
def parse_host(addr, platform=None):
276276
proto = "http+unix"
277277
host = DEFAULT_HTTP_HOST
278278
port = None
279279
path = ''
280+
281+
if not addr and platform == 'win32':
282+
addr = '{0}:{1}'.format(DEFAULT_HTTP_HOST, 2375)
283+
280284
if not addr or addr.strip() == 'unix://':
281285
return DEFAULT_UNIX_SOCKET
282286

tests/utils_test.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ def test_parse_host(self):
7979
'tcp://:7777': 'http://127.0.0.1:7777',
8080
'http://:7777': 'http://127.0.0.1:7777',
8181
'https://kokia.jp:2375': 'https://kokia.jp:2375',
82-
'': 'http+unix://var/run/docker.sock',
83-
None: 'http+unix://var/run/docker.sock',
8482
'unix:///var/run/docker.sock': 'http+unix:///var/run/docker.sock',
8583
'unix://': 'http+unix://var/run/docker.sock',
8684
'somehost.net:80/service/swarm': (
@@ -90,10 +88,20 @@ def test_parse_host(self):
9088

9189
for host in invalid_hosts:
9290
with pytest.raises(DockerException):
93-
parse_host(host)
91+
parse_host(host, None)
9492

9593
for host, expected in valid_hosts.items():
96-
self.assertEqual(parse_host(host), expected, msg=host)
94+
self.assertEqual(parse_host(host, None), expected, msg=host)
95+
96+
def test_parse_host_empty_value(self):
97+
unix_socket = 'http+unix://var/run/docker.sock'
98+
tcp_port = 'http://127.0.0.1:2375'
99+
100+
for val in [None, '']:
101+
for platform in ['darwin', 'linux2', None]:
102+
assert parse_host(val, platform) == unix_socket
103+
104+
assert parse_host(val, 'win32') == tcp_port
97105

98106
def test_kwargs_from_env_empty(self):
99107
os.environ.update(DOCKER_HOST='',

0 commit comments

Comments
 (0)