Skip to content

Commit

Permalink
only determine default gateway within docker container if docker host…
Browse files Browse the repository at this point in the history
… differ from default (#648)
  • Loading branch information
qoomon authored and bsideup committed Apr 19, 2018
1 parent 6a5834e commit 58a24f9
Showing 1 changed file with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ public class DockerClientConfigUtils {
// See https://github.com/docker/docker/blob/a9fa38b1edf30b23cae3eade0be48b3d4b1de14b/daemon/initlayer/setup_unix.go#L25
public static final boolean IN_A_CONTAINER = new File("/.dockerenv").exists();

@Deprecated
@Getter(lazy = true)
private static final Optional<String> detectedDockerHostIp = Optional
.of(IN_A_CONTAINER)
.filter(it -> it)
.map(file -> DockerClientFactory.instance().runInsideDocker(
private static final Optional<String> detectedDockerHostIp = IN_A_CONTAINER ? getDefaultGateway() : Optional.empty();

@Getter(lazy = true)
private static final Optional<String> defaultGateway = Optional
.ofNullable(DockerClientFactory.instance().runInsideDocker(
cmd -> cmd.withCmd("sh", "-c", "ip route|awk '/default/ { print $3 }'"),
(client, id) -> {
try {
Expand All @@ -41,18 +43,21 @@ public class DockerClientConfigUtils {
.map(StringUtils::trimToEmpty)
.filter(StringUtils::isNotBlank);



public static String getDockerHostIpAddress(DockerClientConfig config) {
return getDetectedDockerHostIp().orElseGet(() -> {
switch (config.getDockerHost().getScheme()) {
case "http":
case "https":
case "tcp":
return config.getDockerHost().getHost();
case "unix":
return "localhost";
default:
return null;
}
});
switch (config.getDockerHost().getScheme()) {
case "http":
case "https":
case "tcp":
return config.getDockerHost().getHost();
case "unix":
if (IN_A_CONTAINER) {
return getDefaultGateway().orElse("localhost");
}
return "localhost";
default:
return null;
}
}
}

0 comments on commit 58a24f9

Please sign in to comment.