FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix getDockerHostIpAddress within docker container by qoomon · Pull Request #648 · testcontainers/testcontainers-java · GitHub

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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;
}
}
}

Back | FazBrowse Home | New Git URL