Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.

Commit 93e7f34

Browse files
committed
Merge pull request #228 from spotify/rohan/unix
Default to Unix socket on Linux machines
2 parents 85e8985 + a0b20e8 commit 93e7f34

File tree

1 file changed

+24
-9
lines changed
  • helios-services/src/main/java/com/spotify/helios/servicescommon

1 file changed

+24
-9
lines changed

helios-services/src/main/java/com/spotify/helios/servicescommon/DockerHost.java

+24-9
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,32 @@ public class DockerHost {
3636

3737
public static final int DEFAULT_PORT = 2375;
3838
public static final String DEFAULT_HOST = "localhost";
39+
public static final String DEFAULT_UNIX_ENDPOINT = "unix:///var/run/docker.sock";
3940

4041
private final String host;
4142
private final URI uri;
4243
private final String address;
4344
private final int port;
4445

4546
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+
}
5361
}
5462

5563
/**
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).
5765
*/
5866
public String host() {
5967
return host;
@@ -84,7 +92,14 @@ public String address() {
8492
* Create a {@link DockerHost} from DOCKER_HOST and DOCKER_PORT env vars.
8593
*/
8694
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);
88103
return new DockerHost(host);
89104
}
90105

0 commit comments

Comments
 (0)