@@ -36,24 +36,32 @@ public class DockerHost {
36
36
37
37
public static final int DEFAULT_PORT = 2375 ;
38
38
public static final String DEFAULT_HOST = "localhost" ;
39
+ public static final String DEFAULT_UNIX_ENDPOINT = "unix:///var/run/docker.sock" ;
39
40
40
41
private final String host ;
41
42
private final URI uri ;
42
43
private final String address ;
43
44
private final int port ;
44
45
45
46
private DockerHost (final String endpoint ) {
46
- final String stripped = endpoint .replaceAll (".*://" , "" );
47
- final HostAndPort hostAndPort = HostAndPort .fromString (stripped );
48
- final String hostText = hostAndPort .getHostText ();
49
- this .port = hostAndPort .getPortOrDefault (defaultPort ());
50
- this .address = Strings .isNullOrEmpty (hostText ) ? DEFAULT_HOST : hostText ;
51
- this .host = address + ":" + port ;
52
- this .uri = URI .create ("http://" + address + ":" + port );
47
+ if (endpoint .startsWith ("unix://" )) {
48
+ this .port = 0 ;
49
+ this .address = "localhost" ;
50
+ this .host = endpoint ;
51
+ this .uri = URI .create (endpoint );
52
+ } else {
53
+ final String stripped = endpoint .replaceAll (".*://" , "" );
54
+ final HostAndPort hostAndPort = HostAndPort .fromString (stripped );
55
+ final String hostText = hostAndPort .getHostText ();
56
+ this .port = hostAndPort .getPortOrDefault (defaultPort ());
57
+ this .address = Strings .isNullOrEmpty (hostText ) ? DEFAULT_HOST : hostText ;
58
+ this .host = address + ":" + port ;
59
+ this .uri = URI .create ("http://" + address + ":" + port );
60
+ }
53
61
}
54
62
55
63
/**
56
- * Get a docker endpoint usable as a DOCKER_HOST environment variable .
64
+ * Get a docker endpoint usable for instantiating a new DockerHost with DockerHost.from(endpoint) .
57
65
*/
58
66
public String host () {
59
67
return host ;
@@ -84,7 +92,14 @@ public String address() {
84
92
* Create a {@link DockerHost} from DOCKER_HOST and DOCKER_PORT env vars.
85
93
*/
86
94
public static DockerHost fromEnv () {
87
- final String host = fromNullable (getenv ("DOCKER_HOST" )).or (DEFAULT_HOST + ":" + defaultPort ());
95
+ String defaultEndpoint ;
96
+ if (System .getProperty ("os.name" ).toLowerCase ().equals ("linux" )) {
97
+ defaultEndpoint = DEFAULT_UNIX_ENDPOINT ;
98
+ } else {
99
+ defaultEndpoint = DEFAULT_HOST + ":" + defaultPort ();
100
+ }
101
+
102
+ final String host = fromNullable (getenv ("DOCKER_HOST" )).or (defaultEndpoint );
88
103
return new DockerHost (host );
89
104
}
90
105
0 commit comments